Skip to content

Commit ed03218

Browse files
eparisxjamesmorris
authored andcommitted
security: Protection for exploiting null dereference using mmap
Add a new security check on mmap operations to see if the user is attempting to mmap to low area of the address space. The amount of space protected is indicated by the new proc tunable /proc/sys/vm/mmap_min_addr and defaults to 0, preserving existing behavior. This patch uses a new SELinux security class "memprotect." Policy already contains a number of allow rules like a_t self:process * (unconfined_t being one of them) which mean that putting this check in the process class (its best current fit) would make it useless as all user processes, which we also want to protect against, would be allowed. By taking the memprotect name of the new class it will also make it possible for us to move some of the other memory protect permissions out of 'process' and into the new class next time we bump the policy version number (which I also think is a good future idea) Acked-by: Stephen Smalley <sds@tycho.nsa.gov> Acked-by: Chris Wright <chrisw@sous-sol.org> Signed-off-by: Eric Paris <eparis@redhat.com> Signed-off-by: James Morris <jmorris@namei.org>
1 parent 13bddc2 commit ed03218

13 files changed

Lines changed: 70 additions & 15 deletions

File tree

‎Documentation/sysctl/vm.txt‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ Currently, these files are in /proc/sys/vm:
3131
- min_unmapped_ratio
3232
- min_slab_ratio
3333
- panic_on_oom
34+
- mmap_min_address
3435

3536
==============================================================
3637

@@ -216,3 +217,17 @@ above-mentioned.
216217
The default value is 0.
217218
1 and 2 are for failover of clustering. Please select either
218219
according to your policy of failover.
220+
221+
==============================================================
222+
223+
mmap_min_addr
224+
225+
This file indicates the amount of address space which a user process will
226+
be restricted from mmaping. Since kernel null dereference bugs could
227+
accidentally operate based on the information in the first couple of pages
228+
of memory userspace processes should not be allowed to write to them. By
229+
default this value is set to 0 and no protections will be enforced by the
230+
security module. Setting this value to something like 64k will allow the
231+
vast majority of applications to work correctly and provide defense in depth
232+
against future potential kernel bugs.
233+

‎include/linux/security.h‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ struct xfrm_user_sec_ctx;
7171
extern int cap_netlink_send(struct sock *sk, struct sk_buff *skb);
7272
extern int cap_netlink_recv(struct sk_buff *skb, int cap);
7373

74+
extern unsigned long mmap_min_addr;
7475
/*
7576
* Values used in the task_security_ops calls
7677
*/
@@ -1241,8 +1242,9 @@ struct security_operations {
12411242
int (*file_ioctl) (struct file * file, unsigned int cmd,
12421243
unsigned long arg);
12431244
int (*file_mmap) (struct file * file,
1244-
unsigned long reqprot,
1245-
unsigned long prot, unsigned long flags);
1245+
unsigned long reqprot, unsigned long prot,
1246+
unsigned long flags, unsigned long addr,
1247+
unsigned long addr_only);
12461248
int (*file_mprotect) (struct vm_area_struct * vma,
12471249
unsigned long reqprot,
12481250
unsigned long prot);
@@ -1814,9 +1816,12 @@ static inline int security_file_ioctl (struct file *file, unsigned int cmd,
18141816

18151817
static inline int security_file_mmap (struct file *file, unsigned long reqprot,
18161818
unsigned long prot,
1817-
unsigned long flags)
1819+
unsigned long flags,
1820+
unsigned long addr,
1821+
unsigned long addr_only)
18181822
{
1819-
return security_ops->file_mmap (file, reqprot, prot, flags);
1823+
return security_ops->file_mmap (file, reqprot, prot, flags, addr,
1824+
addr_only);
18201825
}
18211826

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

24902495
static inline int security_file_mmap (struct file *file, unsigned long reqprot,
24912496
unsigned long prot,
2492-
unsigned long flags)
2497+
unsigned long flags,
2498+
unsigned long addr,
2499+
unsigned long addr_only)
24932500
{
24942501
return 0;
24952502
}

‎kernel/sysctl.c‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -949,6 +949,16 @@ static ctl_table vm_table[] = {
949949
.strategy = &sysctl_jiffies,
950950
},
951951
#endif
952+
#ifdef CONFIG_SECURITY
953+
{
954+
.ctl_name = CTL_UNNUMBERED,
955+
.procname = "mmap_min_addr",
956+
.data = &mmap_min_addr,
957+
.maxlen = sizeof(unsigned long),
958+
.mode = 0644,
959+
.proc_handler = &proc_doulongvec_minmax,
960+
},
961+
#endif
952962
#if defined(CONFIG_X86_32) || \
953963
(defined(CONFIG_SUPERH) && defined(CONFIG_VSYSCALL))
954964
{

‎mm/mmap.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1023,10 +1023,10 @@ unsigned long do_mmap_pgoff(struct file * file, unsigned long addr,
10231023
}
10241024
}
10251025

1026-
error = security_file_mmap(file, reqprot, prot, flags);
1026+
error = security_file_mmap(file, reqprot, prot, flags, addr, 0);
10271027
if (error)
10281028
return error;
1029-
1029+
10301030
/* Clear old maps */
10311031
error = -ENOMEM;
10321032
munmap_back:

‎mm/mremap.c‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,10 @@ unsigned long do_mremap(unsigned long addr,
291291
if ((addr <= new_addr) && (addr+old_len) > new_addr)
292292
goto out;
293293

294+
ret = security_file_mmap(0, 0, 0, 0, new_addr, 1);
295+
if (ret)
296+
goto out;
297+
294298
ret = do_munmap(mm, new_addr, new_len);
295299
if (ret)
296300
goto out;
@@ -390,8 +394,13 @@ unsigned long do_mremap(unsigned long addr,
390394

391395
new_addr = get_unmapped_area(vma->vm_file, 0, new_len,
392396
vma->vm_pgoff, map_flags);
393-
ret = new_addr;
394-
if (new_addr & ~PAGE_MASK)
397+
if (new_addr & ~PAGE_MASK) {
398+
ret = new_addr;
399+
goto out;
400+
}
401+
402+
ret = security_file_mmap(0, 0, 0, 0, new_addr, 1);
403+
if (ret)
395404
goto out;
396405
}
397406
ret = move_vma(vma, addr, old_len, new_len, new_addr);

‎mm/nommu.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,7 @@ static int validate_mmap_request(struct file *file,
639639
}
640640

641641
/* allow the security API to have its say */
642-
ret = security_file_mmap(file, reqprot, prot, flags);
642+
ret = security_file_mmap(file, reqprot, prot, flags, addr, 0);
643643
if (ret < 0)
644644
return ret;
645645

‎security/dummy.c‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,8 +420,12 @@ static int dummy_file_ioctl (struct file *file, unsigned int command,
420420

421421
static int dummy_file_mmap (struct file *file, unsigned long reqprot,
422422
unsigned long prot,
423-
unsigned long flags)
423+
unsigned long flags,
424+
unsigned long addr,
425+
unsigned long addr_only)
424426
{
427+
if (addr < mmap_min_addr)
428+
return -EACCES;
425429
return 0;
426430
}
427431

‎security/security.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ extern struct security_operations dummy_security_ops;
2424
extern void security_fixup_ops(struct security_operations *ops);
2525

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

2829
static inline int verify(struct security_operations *ops)
2930
{
@@ -176,4 +177,5 @@ EXPORT_SYMBOL_GPL(register_security);
176177
EXPORT_SYMBOL_GPL(unregister_security);
177178
EXPORT_SYMBOL_GPL(mod_reg_security);
178179
EXPORT_SYMBOL_GPL(mod_unreg_security);
180+
EXPORT_SYMBOL_GPL(mmap_min_addr);
179181
EXPORT_SYMBOL(security_ops);

‎security/selinux/hooks.c‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2569,12 +2569,16 @@ static int file_map_prot_check(struct file *file, unsigned long prot, int shared
25692569
}
25702570

25712571
static int selinux_file_mmap(struct file *file, unsigned long reqprot,
2572-
unsigned long prot, unsigned long flags)
2572+
unsigned long prot, unsigned long flags,
2573+
unsigned long addr, unsigned long addr_only)
25732574
{
2574-
int rc;
2575+
int rc = 0;
2576+
u32 sid = ((struct task_security_struct*)(current->security))->sid;
25752577

2576-
rc = secondary_ops->file_mmap(file, reqprot, prot, flags);
2577-
if (rc)
2578+
if (addr < mmap_min_addr)
2579+
rc = avc_has_perm(sid, sid, SECCLASS_MEMPROTECT,
2580+
MEMPROTECT__MMAP_ZERO, NULL);
2581+
if (rc || addr_only)
25782582
return rc;
25792583

25802584
if (selinux_checkreqprot)

‎security/selinux/include/av_perm_to_string.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,3 +158,4 @@
158158
S_(SECCLASS_KEY, KEY__CREATE, "create")
159159
S_(SECCLASS_DCCP_SOCKET, DCCP_SOCKET__NODE_BIND, "node_bind")
160160
S_(SECCLASS_DCCP_SOCKET, DCCP_SOCKET__NAME_CONNECT, "name_connect")
161+
S_(SECCLASS_MEMPROTECT, MEMPROTECT__MMAP_ZERO, "mmap_zero")

0 commit comments

Comments
 (0)