Skip to contentSkip to conversation

ksys_mount: check for permissions before resource allocation

PATCH NAK Ilya Matveychikov 4 participants 13 messages Jun 5, 2018 – Jun 6, 2018 VFS

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ksys_mount: check for permissions before resource allocation

Early check for mount permissions prevents possible allocation of 3 pages from kmalloc() pool by unpriveledged user which can be used for spraying the kernel heap.

---
 fs/namespace.c | 3 +++
 1 file changed, 3 insertions(+)
fs/namespace.c +3 -0
index 5f75969adff1..1ef8feb2de2a 100644
@@ -3046,6 +3046,9 @@ int ksys_mount(char __user *dev_name, char __user *dir_name, char __user *type,
 	char *kernel_dev;
 	void *options;
 
+	if (!may_mount())
+		return -EPERM;
+
 	kernel_type = copy_mount_string(type);
 	ret = PTR_ERR(kernel_type);
 	if (IS_ERR(kernel_type))

--
2.17.0

#1 lore raw

Just CC’ed to some of maintainers.

$ perl scripts/get_maintainer.pl fs/0001-ksys_mount-check-for-permissions-before-resource-all.patch
Alexander Viro <[email protected]> (maintainer:FILESYSTEMS (VFS and infrastructure))
[email protected] (open list:FILESYSTEMS (VFS and infrastructure))
[email protected] (open list)
Quoted 28 lines · Ilya Matveychikov

Early check for mount permissions prevents possible allocation of 3 pages from kmalloc() pool by unpriveledged user which can be used for spraying the kernel heap.

---
fs/namespace.c | 3 +++

1 file changed, 3 insertions(+)
fs/namespace.c +3 -1
index 5f75969adff1..1ef8feb2de2a 100644
@@ -3046,6 +3046,9 @@ int ksys_mount(char __user *dev_name, char __user *dir_name, char __user *type,
 	char *kernel_dev;
 	void *options;
 
+	if (!may_mount())
+		return -EPERM;
+
 	kernel_type = copy_mount_string(type);
 	ret = PTR_ERR(kernel_type);
 	if (IS_ERR(kernel_type))
--

2.17.0

maintainer
#2 lore raw
Quoted 12 lines · Ilya Matveychikov

Just CC’ed to some of maintainers.

$ perl scripts/get_maintainer.pl fs/0001-ksys_mount-check-for-permissions-before-resource-all.patch
Alexander Viro <[email protected]> (maintainer:FILESYSTEMS (VFS and infrastructure))
[email protected] (open list:FILESYSTEMS (VFS and infrastructure))
[email protected] (open list)
Quoted 5 lines · Ilya Matveychikov

Early check for mount permissions prevents possible allocation of 3 pages from kmalloc() pool by unpriveledged user which can be used for spraying the kernel heap.

I'm sorry, but there are arseloads of unpriveleged syscalls that do the same, starting with read() from procfs files. So what the hell does it buy?

#3 lore raw
Quoted 11 lines · Al Viro
Quoted 6 lines
Quoted 6 lines · Ilya Matveychikov

Early check for mount permissions prevents possible allocation of 3 pages from kmalloc() pool by unpriveledged user which can be used for spraying the kernel heap.

I'm sorry, but there are arseloads of unpriveleged syscalls that do the same, starting with read() from procfs files. So what the hell does it buy?

Means that if all do the same shit no reason to fix it? Sounds weird...

maintainer
#4 lore raw
Quoted 5 lines · Ilya Matveychikov
Quoted 5 lines · Al Viro
Quoted 3 lines
Quoted 3 lines · Ilya Matveychikov
Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
Quoted 3 lines

Early check for mount permissions prevents possible allocation of 3 pages from kmalloc() pool by unpriveledged user which can be used for spraying the kernel heap.

I'm sorry, but there are arseloads of unpriveleged syscalls that do the same, starting with read() from procfs files. So what the hell does it buy?

Means that if all do the same shit no reason to fix it? Sounds weird...

Fix *what*? You do realize that there's no permission checks to stop e.g. stat(2) from copying the pathname in, right? With user-supplied contents, even...

If you depend upon preventing kmalloc'ed temporary allocations filled with user-supplied data, you are screwed, plain and simple. It really can't be prevented, in a lot of ways that are much less exotic than mount(2). Most of syscall arguments are copied in, before we get any permission checks. It does happen and it will happen - examining them while they are still in userland is a nightmare in a lot of respects, starting with security.

#5 lore raw
Quoted 23 lines · Al Viro
Quoted 5 lines · Ilya Matveychikov
Quoted 5 lines · Al Viro
Quoted 3 lines
Quoted 3 lines · Ilya Matveychikov
Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
Quoted 3 lines

Early check for mount permissions prevents possible allocation of 3 pages from kmalloc() pool by unpriveledged user which can be used for spraying the kernel heap.

I'm sorry, but there are arseloads of unpriveleged syscalls that do the same, starting with read() from procfs files. So what the hell does it buy?

Means that if all do the same shit no reason to fix it? Sounds weird...

Fix *what*? You do realize that there's no permission checks to stop e.g. stat(2) from copying the pathname in, right? With user-supplied contents, even...

Quoted 7 lines

If you depend upon preventing kmalloc'ed temporary allocations filled with user-supplied data, you are screwed, plain and simple. It really can't be prevented, in a lot of ways that are much less exotic than mount(2). Most of syscall arguments are copied in, before we get any permission checks. It does happen and it will happen - examining them while they are still in userland is a nightmare in a lot of respects, starting with security.

I agree that it’s impossible to completely avoid this kind of allocations and examining data in user-land will be the bigger problem than copying arguments to the kernel. But aside of that what’s wrong with the idea of having the permission check before doing any kind of work?

BTW, sys_umount() has this check in the right place - before doing anything. So, why not to have the same logic for mount/umount?

#6 lore raw
Quoted 25 lines · Ilya Matveychikov
Quoted 17 lines · Al Viro
Quoted 10 lines · Ilya Matveychikov
Quoted 8 lines · Al Viro
Quoted 4 lines
Quoted 4 lines · Ilya Matveychikov

Early check for mount permissions prevents possible allocation of 3 pages from kmalloc() pool by unpriveledged user which can be used for spraying the kernel heap.

I'm sorry, but there are arseloads of unpriveleged syscalls that do the same, starting with read() from procfs files. So what the hell does it buy?

Means that if all do the same shit no reason to fix it? Sounds weird...

Fix *what*? You do realize that there's no permission checks to stop e.g. stat(2) from copying the pathname in, right? With user-supplied contents, even...

Quoted 7 lines

If you depend upon preventing kmalloc'ed temporary allocations filled with user-supplied data, you are screwed, plain and simple. It really can't be prevented, in a lot of ways that are much less exotic than mount(2). Most of syscall arguments are copied in, before we get any permission checks. It does happen and it will happen - examining them while they are still in userland is a nightmare in a lot of respects, starting with security.

Quoted 4 lines

I agree that it’s impossible to completely avoid this kind of allocations and examining data in user-land will be the bigger problem than copying arguments to the kernel. But aside of that what’s wrong with the idea of having the permission check before doing any kind of work?

Isn't there some sysctl knob or config option to sanitize freed memory? I doubt that using kzfree everywhere unconditionally would be welcome, also would not scale as there are too many of them. This IMHO leaves only the build-time option for those willing to pay the performance hit.

Quoted 2 lines

BTW, sys_umount() has this check in the right place - before doing anything. So, why not to have the same logic for mount/umount?

What if the check is not equivalent to the one done later? may_mount needs namespace, it will be available at umount time but not necessarily during mount due to the security hooks.

#7 lore raw
Quoted 9 lines · David Sterba
Quoted 3 lines

BTW, sys_umount() has this check in the right place - before doing anything. So, why not to have the same logic for mount/umount?

What if the check is not equivalent to the one done later? may_mount needs namespace, it will be available at umount time but not necessarily during mount due to the security hooks.

Might be the issue, you’re right. I can’t tell it for sure as I’m not so familiar with linux/fs code.

maintainer
#8 lore raw
In reply to Ilya Matveychikov
Quoted 8 lines · Ilya Matveychikov
Quoted 7 lines

If you depend upon preventing kmalloc'ed temporary allocations filled with user-supplied data, you are screwed, plain and simple. It really can't be prevented, in a lot of ways that are much less exotic than mount(2). Most of syscall arguments are copied in, before we get any permission checks. It does happen and it will happen - examining them while they are still in userland is a nightmare in a lot of respects, starting with security.

Quoted 4 lines

I agree that it’s impossible to completely avoid this kind of allocations and examining data in user-land will be the bigger problem than copying arguments to the kernel. But aside of that what’s wrong with the idea of having the permission check before doing any kind of work?

Presenting that as mitigating a vulnerability. It's neither better nor worse in that respect than the original.

Nacked-by NAK
#9 lore raw
In reply to Ilya Matveychikov
Quoted 12 lines · Ilya Matveychikov

Just CC’ed to some of maintainers.

$ perl scripts/get_maintainer.pl fs/0001-ksys_mount-check-for-permissions-before-resource-all.patch
Alexander Viro <[email protected]> (maintainer:FILESYSTEMS (VFS and infrastructure))
[email protected] (open list:FILESYSTEMS (VFS and infrastructure))
[email protected] (open list)
Quoted 5 lines · Ilya Matveychikov

Early check for mount permissions prevents possible allocation of 3 pages from kmalloc() pool by unpriveledged user which can be used for spraying the kernel heap.

*Snort*

You clearly have not read may_mount. Your modified code still let's unprivileged users in. So even if all of Al's good objections were not applicable this change would still be buggy and wrong.

Quoted 22 lines
Quoted 22 lines

---
fs/namespace.c | 3 +++

1 file changed, 3 insertions(+)
fs/namespace.c +3 -1
index 5f75969adff1..1ef8feb2de2a 100644
@@ -3046,6 +3046,9 @@ int ksys_mount(char __user *dev_name, char __user *dir_name, char __user *type,
 	char *kernel_dev;
 	void *options;
 
+	if (!may_mount())
+		return -EPERM;
+
 	kernel_type = copy_mount_string(type);
 	ret = PTR_ERR(kernel_type);
 	if (IS_ERR(kernel_type))
--

2.17.0

#10 lore raw
Quoted 19 lines · Eric W. Biederman
Quoted 12 lines · Ilya Matveychikov

Just CC’ed to some of maintainers.

$ perl scripts/get_maintainer.pl fs/0001-ksys_mount-check-for-permissions-before-resource-all.patch
Alexander Viro <[email protected]> (maintainer:FILESYSTEMS (VFS and infrastructure))
[email protected] (open list:FILESYSTEMS (VFS and infrastructure))
[email protected] (open list)
Quoted 5 lines · Ilya Matveychikov

Early check for mount permissions prevents possible allocation of 3 pages from kmalloc() pool by unpriveledged user which can be used for spraying the kernel heap.

Quoted 7 lines

*Snort*

You clearly have not read may_mount. Your modified code still let's unprivileged users in. So even if all of Al's good objections were not applicable this change would still be buggy and wrong.

Don’t get me wrong but may_mount() is:

static inline bool may_mount(void)

{
        return ns_capable(current->nsproxy->mnt_ns->user_ns, CAP_SYS_ADMIN);
}

What do you mean by "You clearly have not read may_mount”? The only thing that can affect may_mount result (as mentioned earlier) is that task’s NS capability might be changed by security_sb_mount() hook.

So, do you think that is’s possible to NOT have CAP_SYS_ADMIN while entering to ksys_mount() but getting it with the security_sb_mount() hook?

This is the only case I see that using may_mount() before security_sb_mount() is wrong. This was the point?

#11 lore raw
Quoted 33 lines · Ilya Matveychikov
Quoted 17 lines · Eric W. Biederman
Quoted 12 lines · Ilya Matveychikov

Just CC’ed to some of maintainers.

$ perl scripts/get_maintainer.pl fs/0001-ksys_mount-check-for-permissions-before-resource-all.patch
Alexander Viro <[email protected]> (maintainer:FILESYSTEMS (VFS and infrastructure))
[email protected] (open list:FILESYSTEMS (VFS and infrastructure))
[email protected] (open list)
Quoted 5 lines · Ilya Matveychikov

Early check for mount permissions prevents possible allocation of 3 pages from kmalloc() pool by unpriveledged user which can be used for spraying the kernel heap.

Quoted 7 lines

*Snort*

You clearly have not read may_mount. Your modified code still let's unprivileged users in. So even if all of Al's good objections were not applicable this change would still be buggy and wrong.

Don’t get me wrong but may_mount() is:

static inline bool may_mount(void)

{
        return ns_capable(current->nsproxy->mnt_ns->user_ns, CAP_SYS_ADMIN);
}
Quoted 6 lines

What do you mean by "You clearly have not read may_mount”? The only thing that can affect may_mount result (as mentioned earlier) is that task’s NS capability might be changed by security_sb_mount() hook.

So, do you think that is’s possible to NOT have CAP_SYS_ADMIN while entering to ksys_mount() but getting it with the security_sb_mount() hook?

I mean it works for unprivileged users.

You can try "unshare -Urm" on a reasonably recent kernel and it will work and you can then mount and unmount things.

Strictly speaking it only works if you have the appropriate set of capabilities in your user namespace. But that does not imply you are a privileged user in the broader sense.

Any user can create a user namespace, and become the root user in a user namespace. The root user in a user namespace can create a mount namespace. The root user in a user namespace can mount and unmount filesystems in their namespace.

Or in net anyone can call mount and get past the may_mount test.

Without reducing who can cause the kernel allocation moving the test is pointless.

Eric

#12 lore raw
Quoted 60 lines · Eric W. Biederman
Quoted 33 lines · Ilya Matveychikov
Quoted 17 lines · Eric W. Biederman
Quoted 12 lines · Ilya Matveychikov

Just CC’ed to some of maintainers.

$ perl scripts/get_maintainer.pl fs/0001-ksys_mount-check-for-permissions-before-resource-all.patch
Alexander Viro <[email protected]> (maintainer:FILESYSTEMS (VFS and infrastructure))
[email protected] (open list:FILESYSTEMS (VFS and infrastructure))
[email protected] (open list)
Quoted 5 lines · Ilya Matveychikov

Early check for mount permissions prevents possible allocation of 3 pages from kmalloc() pool by unpriveledged user which can be used for spraying the kernel heap.

Quoted 7 lines

*Snort*

You clearly have not read may_mount. Your modified code still let's unprivileged users in. So even if all of Al's good objections were not applicable this change would still be buggy and wrong.

Don’t get me wrong but may_mount() is:

static inline bool may_mount(void)

{
       return ns_capable(current->nsproxy->mnt_ns->user_ns, CAP_SYS_ADMIN);
}
Quoted 6 lines

What do you mean by "You clearly have not read may_mount”? The only thing that can affect may_mount result (as mentioned earlier) is that task’s NS capability might be changed by security_sb_mount() hook.

So, do you think that is’s possible to NOT have CAP_SYS_ADMIN while entering to ksys_mount() but getting it with the security_sb_mount() hook?

I mean it works for unprivileged users.

You can try "unshare -Urm" on a reasonably recent kernel and it will work and you can then mount and unmount things.

Strictly speaking it only works if you have the appropriate set of capabilities in your user namespace. But that does not imply you are a privileged user in the broader sense.

Any user can create a user namespace, and become the root user in a user namespace. The root user in a user namespace can create a mount namespace. The root user in a user namespace can mount and unmount filesystems in their namespace.

Quoted 5 lines

Or in net anyone can call mount and get past the may_mount test.

Without reducing who can cause the kernel allocation moving the test is pointless.

Ok, now I see. No reason to make change as it doesn’t really prevents users of doing mount() using they own namespaces.

Thank you for the explanation.