Skip to content

Commit

Permalink
Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel…
Browse files Browse the repository at this point in the history
…/git/jmorris/selinux-2.6

* 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jmorris/selinux-2.6:
  security: unexport mmap_min_addr
  SELinux: use SECINITSID_NETMSG instead of SECINITSID_UNLABELED for NetLabel
  security: Protection for exploiting null dereference using mmap
  SELinux: Use %lu for inode->i_no when printing avc
  SELinux: allow preemption between transition permission checks
  selinux: introduce schedule points in policydb_destroy()
  selinux: add selinuxfs structure for object class discovery
  selinux: change sel_make_dir() to specify inode counter.
  selinux: rename sel_remove_bools() for more general usage.
  selinux: add support for querying object classes and permissions from the running policy
  • Loading branch information
Linus Torvalds committed Jul 12, 2007
2 parents 702ed6e + d4cf291 commit bb50cbb
Show file tree
Hide file tree
Showing 20 changed files with 504 additions and 86 deletions.
15 changes: 15 additions & 0 deletions Documentation/sysctl/vm.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Currently, these files are in /proc/sys/vm:
- min_unmapped_ratio
- min_slab_ratio
- panic_on_oom
- mmap_min_address

==============================================================

Expand Down Expand Up @@ -216,3 +217,17 @@ above-mentioned.
The default value is 0.
1 and 2 are for failover of clustering. Please select either
according to your policy of failover.

==============================================================

mmap_min_addr

This file indicates the amount of address space which a user process will
be restricted from mmaping. Since kernel null dereference bugs could
accidentally operate based on the information in the first couple of pages
of memory userspace processes should not be allowed to write to them. By
default this value is set to 0 and no protections will be enforced by the
security module. Setting this value to something like 64k will allow the
vast majority of applications to work correctly and provide defense in depth
against future potential kernel bugs.

17 changes: 12 additions & 5 deletions include/linux/security.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ struct xfrm_user_sec_ctx;
extern int cap_netlink_send(struct sock *sk, struct sk_buff *skb);
extern int cap_netlink_recv(struct sk_buff *skb, int cap);

extern unsigned long mmap_min_addr;
/*
* Values used in the task_security_ops calls
*/
Expand Down Expand Up @@ -1241,8 +1242,9 @@ struct security_operations {
int (*file_ioctl) (struct file * file, unsigned int cmd,
unsigned long arg);
int (*file_mmap) (struct file * file,
unsigned long reqprot,
unsigned long prot, unsigned long flags);
unsigned long reqprot, unsigned long prot,
unsigned long flags, unsigned long addr,
unsigned long addr_only);
int (*file_mprotect) (struct vm_area_struct * vma,
unsigned long reqprot,
unsigned long prot);
Expand Down Expand Up @@ -1814,9 +1816,12 @@ static inline int security_file_ioctl (struct file *file, unsigned int cmd,

static inline int security_file_mmap (struct file *file, unsigned long reqprot,
unsigned long prot,
unsigned long flags)
unsigned long flags,
unsigned long addr,
unsigned long addr_only)
{
return security_ops->file_mmap (file, reqprot, prot, flags);
return security_ops->file_mmap (file, reqprot, prot, flags, addr,
addr_only);
}

static inline int security_file_mprotect (struct vm_area_struct *vma,
Expand Down Expand Up @@ -2489,7 +2494,9 @@ static inline int security_file_ioctl (struct file *file, unsigned int cmd,

static inline int security_file_mmap (struct file *file, unsigned long reqprot,
unsigned long prot,
unsigned long flags)
unsigned long flags,
unsigned long addr,
unsigned long addr_only)
{
return 0;
}
Expand Down
10 changes: 10 additions & 0 deletions kernel/sysctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -949,6 +949,16 @@ static ctl_table vm_table[] = {
.strategy = &sysctl_jiffies,
},
#endif
#ifdef CONFIG_SECURITY
{
.ctl_name = CTL_UNNUMBERED,
.procname = "mmap_min_addr",
.data = &mmap_min_addr,
.maxlen = sizeof(unsigned long),
.mode = 0644,
.proc_handler = &proc_doulongvec_minmax,
},
#endif
#if defined(CONFIG_X86_32) || \
(defined(CONFIG_SUPERH) && defined(CONFIG_VSYSCALL))
{
Expand Down
4 changes: 2 additions & 2 deletions mm/mmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -1023,10 +1023,10 @@ unsigned long do_mmap_pgoff(struct file * file, unsigned long addr,
}
}

error = security_file_mmap(file, reqprot, prot, flags);
error = security_file_mmap(file, reqprot, prot, flags, addr, 0);
if (error)
return error;

/* Clear old maps */
error = -ENOMEM;
munmap_back:
Expand Down
13 changes: 11 additions & 2 deletions mm/mremap.c
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,10 @@ unsigned long do_mremap(unsigned long addr,
if ((addr <= new_addr) && (addr+old_len) > new_addr)
goto out;

ret = security_file_mmap(0, 0, 0, 0, new_addr, 1);
if (ret)
goto out;

ret = do_munmap(mm, new_addr, new_len);
if (ret)
goto out;
Expand Down Expand Up @@ -390,8 +394,13 @@ unsigned long do_mremap(unsigned long addr,

new_addr = get_unmapped_area(vma->vm_file, 0, new_len,
vma->vm_pgoff, map_flags);
ret = new_addr;
if (new_addr & ~PAGE_MASK)
if (new_addr & ~PAGE_MASK) {
ret = new_addr;
goto out;
}

ret = security_file_mmap(0, 0, 0, 0, new_addr, 1);
if (ret)
goto out;
}
ret = move_vma(vma, addr, old_len, new_len, new_addr);
Expand Down
2 changes: 1 addition & 1 deletion mm/nommu.c
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,7 @@ static int validate_mmap_request(struct file *file,
}

/* allow the security API to have its say */
ret = security_file_mmap(file, reqprot, prot, flags);
ret = security_file_mmap(file, reqprot, prot, flags, addr, 0);
if (ret < 0)
return ret;

Expand Down
6 changes: 5 additions & 1 deletion security/dummy.c
Original file line number Diff line number Diff line change
Expand Up @@ -420,8 +420,12 @@ static int dummy_file_ioctl (struct file *file, unsigned int command,

static int dummy_file_mmap (struct file *file, unsigned long reqprot,
unsigned long prot,
unsigned long flags)
unsigned long flags,
unsigned long addr,
unsigned long addr_only)
{
if (addr < mmap_min_addr)
return -EACCES;
return 0;
}

Expand Down
1 change: 1 addition & 0 deletions security/security.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ extern struct security_operations dummy_security_ops;
extern void security_fixup_ops(struct security_operations *ops);

struct security_operations *security_ops; /* Initialized to NULL */
unsigned long mmap_min_addr; /* 0 means no protection */

static inline int verify(struct security_operations *ops)
{
Expand Down
12 changes: 7 additions & 5 deletions security/selinux/avc.c
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ void avc_audit(u32 ssid, u32 tsid,
}
}
if (inode)
audit_log_format(ab, " dev=%s ino=%ld",
audit_log_format(ab, " dev=%s ino=%lu",
inode->i_sb->s_id,
inode->i_ino);
break;
Expand Down Expand Up @@ -832,6 +832,7 @@ int avc_ss_reset(u32 seqno)
* @tsid: target security identifier
* @tclass: target security class
* @requested: requested permissions, interpreted based on @tclass
* @flags: AVC_STRICT or 0
* @avd: access vector decisions
*
* Check the AVC to determine whether the @requested permissions are granted
Expand All @@ -846,8 +847,9 @@ int avc_ss_reset(u32 seqno)
* should be released for the auditing.
*/
int avc_has_perm_noaudit(u32 ssid, u32 tsid,
u16 tclass, u32 requested,
struct av_decision *avd)
u16 tclass, u32 requested,
unsigned flags,
struct av_decision *avd)
{
struct avc_node *node;
struct avc_entry entry, *p_ae;
Expand All @@ -874,7 +876,7 @@ int avc_has_perm_noaudit(u32 ssid, u32 tsid,
denied = requested & ~(p_ae->avd.allowed);

if (!requested || denied) {
if (selinux_enforcing)
if (selinux_enforcing || (flags & AVC_STRICT))
rc = -EACCES;
else
if (node)
Expand Down Expand Up @@ -909,7 +911,7 @@ int avc_has_perm(u32 ssid, u32 tsid, u16 tclass,
struct av_decision avd;
int rc;

rc = avc_has_perm_noaudit(ssid, tsid, tclass, requested, &avd);
rc = avc_has_perm_noaudit(ssid, tsid, tclass, requested, 0, &avd);
avc_audit(ssid, tsid, tclass, requested, &avd, rc, auditdata);
return rc;
}
42 changes: 24 additions & 18 deletions security/selinux/hooks.c
Original file line number Diff line number Diff line change
Expand Up @@ -1592,9 +1592,10 @@ static int selinux_vm_enough_memory(long pages)
rc = secondary_ops->capable(current, CAP_SYS_ADMIN);
if (rc == 0)
rc = avc_has_perm_noaudit(tsec->sid, tsec->sid,
SECCLASS_CAPABILITY,
CAP_TO_MASK(CAP_SYS_ADMIN),
NULL);
SECCLASS_CAPABILITY,
CAP_TO_MASK(CAP_SYS_ADMIN),
0,
NULL);

if (rc == 0)
cap_sys_admin = 1;
Expand Down Expand Up @@ -2568,12 +2569,16 @@ static int file_map_prot_check(struct file *file, unsigned long prot, int shared
}

static int selinux_file_mmap(struct file *file, unsigned long reqprot,
unsigned long prot, unsigned long flags)
unsigned long prot, unsigned long flags,
unsigned long addr, unsigned long addr_only)
{
int rc;
int rc = 0;
u32 sid = ((struct task_security_struct*)(current->security))->sid;

rc = secondary_ops->file_mmap(file, reqprot, prot, flags);
if (rc)
if (addr < mmap_min_addr)
rc = avc_has_perm(sid, sid, SECCLASS_MEMPROTECT,
MEMPROTECT__MMAP_ZERO, NULL);
if (rc || addr_only)
return rc;

if (selinux_checkreqprot)
Expand Down Expand Up @@ -3124,28 +3129,29 @@ static int selinux_parse_skb(struct sk_buff *skb, struct avc_audit_data *ad,
/**
* selinux_skb_extlbl_sid - Determine the external label of a packet
* @skb: the packet
* @base_sid: the SELinux SID to use as a context for MLS only external labels
* @sid: the packet's SID
*
* Description:
* Check the various different forms of external packet labeling and determine
* the external SID for the packet.
* the external SID for the packet. If only one form of external labeling is
* present then it is used, if both labeled IPsec and NetLabel labels are
* present then the SELinux type information is taken from the labeled IPsec
* SA and the MLS sensitivity label information is taken from the NetLabel
* security attributes. This bit of "magic" is done in the call to
* selinux_netlbl_skbuff_getsid().
*
*/
static void selinux_skb_extlbl_sid(struct sk_buff *skb,
u32 base_sid,
u32 *sid)
static void selinux_skb_extlbl_sid(struct sk_buff *skb, u32 *sid)
{
u32 xfrm_sid;
u32 nlbl_sid;

selinux_skb_xfrm_sid(skb, &xfrm_sid);
if (selinux_netlbl_skbuff_getsid(skb,
(xfrm_sid == SECSID_NULL ?
base_sid : xfrm_sid),
SECINITSID_NETMSG : xfrm_sid),
&nlbl_sid) != 0)
nlbl_sid = SECSID_NULL;

*sid = (nlbl_sid == SECSID_NULL ? xfrm_sid : nlbl_sid);
}

Expand Down Expand Up @@ -3690,7 +3696,7 @@ static int selinux_socket_getpeersec_dgram(struct socket *sock, struct sk_buff *
if (sock && sock->sk->sk_family == PF_UNIX)
selinux_get_inode_sid(SOCK_INODE(sock), &peer_secid);
else if (skb)
selinux_skb_extlbl_sid(skb, SECINITSID_UNLABELED, &peer_secid);
selinux_skb_extlbl_sid(skb, &peer_secid);

if (peer_secid == SECSID_NULL)
err = -EINVAL;
Expand Down Expand Up @@ -3751,7 +3757,7 @@ static int selinux_inet_conn_request(struct sock *sk, struct sk_buff *skb,
u32 newsid;
u32 peersid;

selinux_skb_extlbl_sid(skb, SECINITSID_UNLABELED, &peersid);
selinux_skb_extlbl_sid(skb, &peersid);
if (peersid == SECSID_NULL) {
req->secid = sksec->sid;
req->peer_secid = SECSID_NULL;
Expand Down Expand Up @@ -3789,7 +3795,7 @@ static void selinux_inet_conn_established(struct sock *sk,
{
struct sk_security_struct *sksec = sk->sk_security;

selinux_skb_extlbl_sid(skb, SECINITSID_UNLABELED, &sksec->peer_sid);
selinux_skb_extlbl_sid(skb, &sksec->peer_sid);
}

static void selinux_req_classify_flow(const struct request_sock *req,
Expand Down Expand Up @@ -4626,7 +4632,7 @@ static int selinux_setprocattr(struct task_struct *p,
if (p->ptrace & PT_PTRACED) {
error = avc_has_perm_noaudit(tsec->ptrace_sid, sid,
SECCLASS_PROCESS,
PROCESS__PTRACE, &avd);
PROCESS__PTRACE, 0, &avd);
if (!error)
tsec->sid = sid;
task_unlock(p);
Expand Down
1 change: 1 addition & 0 deletions security/selinux/include/av_perm_to_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,4 @@
S_(SECCLASS_KEY, KEY__CREATE, "create")
S_(SECCLASS_DCCP_SOCKET, DCCP_SOCKET__NODE_BIND, "node_bind")
S_(SECCLASS_DCCP_SOCKET, DCCP_SOCKET__NAME_CONNECT, "name_connect")
S_(SECCLASS_MEMPROTECT, MEMPROTECT__MMAP_ZERO, "mmap_zero")
1 change: 1 addition & 0 deletions security/selinux/include/av_permissions.h
Original file line number Diff line number Diff line change
Expand Up @@ -823,3 +823,4 @@
#define DCCP_SOCKET__NAME_BIND 0x00200000UL
#define DCCP_SOCKET__NODE_BIND 0x00400000UL
#define DCCP_SOCKET__NAME_CONNECT 0x00800000UL
#define MEMPROTECT__MMAP_ZERO 0x00000001UL
6 changes: 4 additions & 2 deletions security/selinux/include/avc.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,11 @@ void avc_audit(u32 ssid, u32 tsid,
u16 tclass, u32 requested,
struct av_decision *avd, int result, struct avc_audit_data *auditdata);

#define AVC_STRICT 1 /* Ignore permissive mode. */
int avc_has_perm_noaudit(u32 ssid, u32 tsid,
u16 tclass, u32 requested,
struct av_decision *avd);
u16 tclass, u32 requested,
unsigned flags,
struct av_decision *avd);

int avc_has_perm(u32 ssid, u32 tsid,
u16 tclass, u32 requested,
Expand Down
1 change: 1 addition & 0 deletions security/selinux/include/class_to_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,4 @@
S_("key")
S_(NULL)
S_("dccp_socket")
S_("memprotect")
1 change: 1 addition & 0 deletions security/selinux/include/flask.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
#define SECCLASS_PACKET 57
#define SECCLASS_KEY 58
#define SECCLASS_DCCP_SOCKET 60
#define SECCLASS_MEMPROTECT 61

/*
* Security identifier indices for initial entities
Expand Down
4 changes: 4 additions & 0 deletions security/selinux/include/security.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ extern int selinux_mls_enabled;

int security_load_policy(void * data, size_t len);

#define SEL_VEC_MAX 32
struct av_decision {
u32 allowed;
u32 decided;
Expand Down Expand Up @@ -87,6 +88,9 @@ int security_validate_transition(u32 oldsid, u32 newsid, u32 tasksid,

int security_sid_mls_copy(u32 sid, u32 mls_sid, u32 *new_sid);

int security_get_classes(char ***classes, int *nclasses);
int security_get_permissions(char *class, char ***perms, int *nperms);

#define SECURITY_FS_USE_XATTR 1 /* use xattr */
#define SECURITY_FS_USE_TRANS 2 /* use transition SIDs, e.g. devpts/tmpfs */
#define SECURITY_FS_USE_TASK 3 /* use task SIDs, e.g. pipefs/sockfs */
Expand Down
Loading

0 comments on commit bb50cbb

Please sign in to comment.