proc: use vmalloc for our kernel buffer
Subsystems (6)
PATCH 1/1 proc: use vmalloc for our kernel buffer
Since
sysctl: pass kernel pointers to ->proc_handler
we have been pre-allocating a buffer to copy the data from the proc handlers into, and then copying that to userspace. The problem is this just blind kmalloc()'s the buffer size passed in from the read, which in the case of our 'cat' binary was 64kib. Order-4 allocations are not awesome, and since we can potentially allocate up to our maximum order, use vmalloc for these buffers.
--- fs/proc/proc_sysctl.c | 6 +++--- include/linux/string.h | 1 + mm/util.c | 26 ++++++++++++++++++++++++++ 3 files changed, 30 insertions(+), 3 deletions(-)
fs/proc/proc_sysctl.c +3 -3
@@ -571,13 +571,13 @@ static ssize_t proc_sys_call_handler(struct file *filp, void __user *ubuf, goto out;if (write) { - kbuf =memdup_user_nul (ubuf, count); + kbuf =vmemdup_user_nul (ubuf, count);if (IS_ERR (kbuf)) { error =PTR_ERR (kbuf); goto out; } }else { - kbuf =kzalloc (count,GFP_KERNEL ); + kbuf =kvzalloc (count,GFP_KERNEL );if (!kbuf) goto out; }
@@ -600,7 +600,7 @@ static ssize_t proc_sys_call_handler(struct file *filp, void __user *ubuf, error = count; out_free_buf: -kfree (kbuf); +kvfree (kbuf); out:sysctl_head_finish (head);
include/linux/string.h +1 -0
@@ -12,6 +12,7 @@extern char *strndup_user (const char __user *,long );extern void *memdup_user (const void __user *,size_t );extern void *vmemdup_user (const void __user *,size_t ); +extern void *vmemdup_user_nul (const void __user *,size_t );extern void *memdup_user_nul (const void __user *,size_t );/*
mm/util.c +26 -0
@@ -208,6 +208,32 @@ void *vmemdup_user(const void __user *src, size_t len) }EXPORT_SYMBOL (vmemdup_user); +/** +* vmemdup_user - duplicate memory region from user space and NUL-terminate +* +* @src: source address in user space +* @len: number of bytes to copy +* +* Return: an ERR_PTR() on failure. Result may be not +* physically contiguous. Use kvfree() to free. +*/ +void *vmemdup_user_nul (const void __user *src,size_t len) +{ +void *p; + + p =kvmalloc (len,GFP_USER ); +if (!p) +return ERR_PTR (-ENOMEM ); + +if (copy_from_user (p, src, len)) { +kvfree (p); +return ERR_PTR (-EFAULT ); + } + +return p; +} +EXPORT_SYMBOL (vmemdup_user_nul); +/** * strndup_user - duplicate an existing string from user space * @s: The string to duplicate
Signature · 1 line
2.24.1
Quoted 2 lines · Josef Bacik
+/**
+ *vmemdup_user- duplicate memory region from user space and NUL-terminate
vmemdup_user_nul()
mm/util.c void *vmemdup_user(const void __user *src, size_t len) · line 224
Quoted 5 lines
mm/util.c +5
-0+void *vmemdup_user_nul (const void __user *src,size_t len) +{ +void *p; + + p =kvmalloc (len,GFP_USER );
len+1, shirley?
mm/util.c void *vmemdup_user(const void __user *src, size_t len) · line 231
Quoted 7 lines
mm/util.c +7
-0+if (!p) +return ERR_PTR (-ENOMEM ); + +if (copy_from_user (p, src, len)) { +kvfree (p); +return ERR_PTR (-EFAULT ); + }
I think you forgot
p[len] = '\0';
Quoted 10 lines
mm/util.c +4
-0+return p; +} +EXPORT_SYMBOL (vmemdup_user_nul); +/** * strndup_user - duplicate an existing string from user space * @s: The string to duplicate Signature · 1 line
2.24.1
Quoted 24 lines · Matthew Wilcox
Quoted 2 lines · Josef Bacik
+/**
+ *vmemdup_user- duplicate memory region from user space and NUL-terminate
vmemdup_user_nul()mm/util.c void *vmemdup_user(const void __user *src, size_t len) · line 224
Quoted 5 lines
mm/util.c +5
-0+void *vmemdup_user_nul (const void __user *src,size_t len) +{ +void *p; + + p =kvmalloc (len,GFP_USER );len+1, shirley?
mm/util.c void *vmemdup_user(const void __user *src, size_t len) · line 231
Quoted 7 lines
mm/util.c +7
-0+if (!p) +return ERR_PTR (-ENOMEM ); + +if (copy_from_user (p, src, len)) { +kvfree (p); +return ERR_PTR (-EFAULT ); + }I think you forgot
Quoted 2 lines
p[len] = '\0';
Sweet lord I need more sleep, my bad. Thanks,
Josef
PATCH v2 1/1 proc: use vmalloc for our kernel buffer
In reply to Josef BacikSince
sysctl: pass kernel pointers to ->proc_handler
we have been pre-allocating a buffer to copy the data from the proc handlers into, and then copying that to userspace. The problem is this just blind kmalloc()'s the buffer size passed in from the read, which in the case of our 'cat' binary was 64kib. Order-4 allocations are not awesome, and since we can potentially allocate up to our maximum order, use vmalloc for these buffers.
---v1->v2:
- Make vmemdup_user_nul actually do the right thing...sorry about that.
fs/proc/proc_sysctl.c | 6 +++--- include/linux/string.h | 1 + mm/util.c | 27 +++++++++++++++++++++++++++ 3 files changed, 31 insertions(+), 3 deletions(-)
fs/proc/proc_sysctl.c +3 -3
@@ -571,13 +571,13 @@ static ssize_t proc_sys_call_handler(struct file *filp, void __user *ubuf, goto out;if (write) { - kbuf =memdup_user_nul (ubuf, count); + kbuf =vmemdup_user_nul (ubuf, count);if (IS_ERR (kbuf)) { error =PTR_ERR (kbuf); goto out; } }else { - kbuf =kzalloc (count,GFP_KERNEL ); + kbuf =kvzalloc (count,GFP_KERNEL );if (!kbuf) goto out; }
@@ -600,7 +600,7 @@ static ssize_t proc_sys_call_handler(struct file *filp, void __user *ubuf, error = count; out_free_buf: -kfree (kbuf); +kvfree (kbuf); out:sysctl_head_finish (head);
include/linux/string.h +1 -0
@@ -12,6 +12,7 @@extern char *strndup_user (const char __user *,long );extern void *memdup_user (const void __user *,size_t );extern void *vmemdup_user (const void __user *,size_t ); +extern void *vmemdup_user_nul (const void __user *,size_t );extern void *memdup_user_nul (const void __user *,size_t );/*
mm/util.c +27 -0
@@ -208,6 +208,33 @@ void *vmemdup_user(const void __user *src, size_t len) }EXPORT_SYMBOL (vmemdup_user); +/** +* vmemdup_user_nul - duplicate memory region from user space and NUL-terminate +* +* @src: source address in user space +* @len: number of bytes to copy +* +* Return: an ERR_PTR() on failure. Result may be not +* physically contiguous. Use kvfree() to free. +*/ +void *vmemdup_user_nul (const void __user *src,size_t len) +{ +char *p; + + p =kvmalloc (len +1 ,GFP_USER ); +if (!p) +return ERR_PTR (-ENOMEM ); + +if (copy_from_user (p, src, len)) { +kvfree (p); +return ERR_PTR (-EFAULT ); + } + p[len] ='\0' ; + +return p; +} +EXPORT_SYMBOL (vmemdup_user_nul); +/** * strndup_user - duplicate an existing string from user space * @s: The string to duplicate
Signature · 1 line
2.24.1
Quoted 27 lines · Josef Bacik
Since
sysctl: pass kernel pointers to ->proc_handler
we have been pre-allocating a buffer to copy the data from the proc handlers into, and then copying that to userspace. The problem is this just blind kmalloc()'s the buffer size passed in from the read, which in the case of our 'cat' binary was 64kib. Order-4 allocations are not awesome, and since we can potentially allocate up to our maximum order, use vmalloc for these buffers.
Fixes 32927393dc1c ("sysctl: pass kernel pointers to ->proc_handler")Signed-off-by Josef Bacik <[email protected]>---
v1->v2:
- Makevmemdup_user_nulactually do the right thing...sorry about that.fs/proc/proc_sysctl.c | 6 +++--- include/linux/string.h | 1 + mm/util.c | 27 +++++++++++++++++++++++++++ 3 files changed, 31 insertions(+), 3 deletions(-)fs/proc/proc_sysctl.c +0
-0
fs/proc/proc_sysctl.c static ssize_t proc_sys_call_handler(struct file *filp, void __user *ubuf, · line 574
Quoted 5 lines
fs/proc/proc_sysctl.c +1
-1@@ -571,13 +571,13 @@ static ssize_t proc_sys_call_handler(struct file *filp, void __user *ubuf, goto out;if (write) { - kbuf =memdup_user_nul (ubuf, count); + kbuf =vmemdup_user_nul (ubuf, count);
Given that this can also do a kmalloc and thus needs to be paired with kvfree shouldn't it be kvmemdup_user_nul?
Quoted 30 lines · Christoph Hellwig
Quoted 29 lines · Josef Bacik
Since
sysctl: pass kernel pointers to ->proc_handler
we have been pre-allocating a buffer to copy the data from the proc handlers into, and then copying that to userspace. The problem is this just blind kmalloc()'s the buffer size passed in from the read, which in the case of our 'cat' binary was 64kib. Order-4 allocations are not awesome, and since we can potentially allocate up to our maximum order, use vmalloc for these buffers.
Fixes 32927393dc1c ("sysctl: pass kernel pointers to ->proc_handler")Signed-off-by Josef Bacik <[email protected]>---
v1->v2:
- Makevmemdup_user_nulactually do the right thing...sorry about that.fs/proc/proc_sysctl.c | 6 +++--- include/linux/string.h | 1 + mm/util.c | 27 +++++++++++++++++++++++++++ 3 files changed, 31 insertions(+), 3 deletions(-)fs/proc/proc_sysctl.c +0
-0@@ -571,13 +571,13 @@ static ssize_t proc_sys_call_handler(struct file *filp, void __user *ubuf, goto out;
Quoted 7 lines
fs/proc/proc_sysctl.c static ssize_t proc_sys_call_handler(struct file *filp, void __user *ubuf, · line 574
Quoted 3 lines
fs/proc/proc_sysctl.c +1
-1if (write) { - kbuf =memdup_user_nul (ubuf, count); + kbuf =vmemdup_user_nul (ubuf, count);Given that this can also do a kmalloc and thus needs to be paired with kvfree shouldn't it be
kvmemdup_user_nul?
There's an existing vmemdup_user that does kvmalloc, so I followed the existing naming convention. Do you want me to change them both? Thanks,
Josef
Quoted 38 lines · Josef Bacik
Quoted 23 lines · Christoph Hellwig
Quoted 22 lines · Josef Bacik
Since
sysctl: pass kernel pointers to ->proc_handler
we have been pre-allocating a buffer to copy the data from the proc handlers into, and then copying that to userspace. The problem is this just blind kmalloc()'s the buffer size passed in from the read, which in the case of our 'cat' binary was 64kib. Order-4 allocations are not awesome, and since we can potentially allocate up to our maximum order, use vmalloc for these buffers.
Fixes 32927393dc1c ("sysctl: pass kernel pointers to ->proc_handler")Signed-off-by Josef Bacik <[email protected]>---
v1->v2:
- Makevmemdup_user_nulactually do the right thing...sorry about that.fs/proc/proc_sysctl.c | 6 +++--- include/linux/string.h | 1 + mm/util.c | 27 +++++++++++++++++++++++++++ 3 files changed, 31 insertions(+), 3 deletions(-)Quoted 13 lines
fs/proc/proc_sysctl.c static ssize_t proc_sys_call_handler(struct file *filp, void __user *ubuf, · line 573
Quoted 9 lines
fs/proc/proc_sysctl.c +1
-1@@ -571,13 +571,13 @@ static ssize_t proc_sys_call_handler(struct file *filp, void __user *ubuf, goto out;if (write) { - kbuf =memdup_user_nul (ubuf, count); + kbuf =vmemdup_user_nul (ubuf, count);Given that this can also do a kmalloc and thus needs to be paired with kvfree shouldn't it be
kvmemdup_user_nul?
Quoted 2 lines
There's an existing
vmemdup_userthat does kvmalloc, so I followed the existing naming convention. Do you want me to change them both? Thanks,
I personally would, and given that it only has a few users it might even be feasible.
Al Viro
maintainerQuoted 39 lines · Christoph Hellwig
Quoted 38 lines · Josef Bacik
Quoted 36 lines · Christoph Hellwig
Quoted 27 lines · Josef Bacik
Since
sysctl: pass kernel pointers to ->proc_handler
we have been pre-allocating a buffer to copy the data from the proc handlers into, and then copying that to userspace. The problem is this just blind kmalloc()'s the buffer size passed in from the read, which in the case of our 'cat' binary was 64kib. Order-4 allocations are not awesome, and since we can potentially allocate up to our maximum order, use vmalloc for these buffers.
Fixes 32927393dc1c ("sysctl: pass kernel pointers to ->proc_handler")Signed-off-by Josef Bacik <[email protected]>---
v1->v2:
- Makevmemdup_user_nulactually do the right thing...sorry about that.fs/proc/proc_sysctl.c | 6 +++--- include/linux/string.h | 1 + mm/util.c | 27 +++++++++++++++++++++++++++ 3 files changed, 31 insertions(+), 3 deletions(-)fs/proc/proc_sysctl.c +0
-0fs/proc/proc_sysctl.c static ssize_t proc_sys_call_handler(struct file *filp, void __user *ubuf, · line 573
Quoted 4 lines
fs/proc/proc_sysctl.c +1
-1@@ -571,13 +571,13 @@ static ssize_t proc_sys_call_handler(struct file *filp, void __user *ubuf, goto out;if (write) { - kbuf =memdup_user_nul (ubuf, count); + kbuf =vmemdup_user_nul (ubuf, count);Given that this can also do a kmalloc and thus needs to be paired with kvfree shouldn't it be
kvmemdup_user_nul?
Quoted 5 lines
Quoted 2 lines
There's an existing
vmemdup_userthat does kvmalloc, so I followed the existing naming convention. Do you want me to change them both? Thanks,I personally would, and given that it only has a few users it might even be feasible.
FWIW, how about following or combining that with "allocate count + 1 bytes on the read side"? Allows some nice cleanups - e.g.
len = sprintf(tmpbuf, "0x%04x", *(unsigned int *) table->data);
if (len > left)
len = left;
memcpy(buffer, tmpbuf, len);
if ((left -= len) > 0) {
*((char *)buffer + len) = '\n';
left--;
}
in sunrpc proc_dodebug() turns into
left -= snprintf(buffer, left, "0x%04x\n",
*(unsigned int *) table->data);and that's not the only example.
Quoted 59 lines · Al Viro
Quoted 39 lines · Christoph Hellwig
Quoted 38 lines · Josef Bacik
Quoted 36 lines · Christoph Hellwig
Quoted 27 lines · Josef Bacik
Since
sysctl: pass kernel pointers to ->proc_handlerwe have been pre-allocating a buffer to copy the data from the proc handlers into, and then copying that to userspace. The problem is this just blind kmalloc()'s the buffer size passed in from the read, which in the case of our 'cat' binary was 64kib. Order-4 allocations are not awesome, and since we can potentially allocate up to our maximum order, use vmalloc for these buffers.
Fixes 32927393dc1c ("sysctl: pass kernel pointers to ->proc_handler")Signed-off-by Josef Bacik <[email protected]>---
v1->v2:
- Makevmemdup_user_nulactually do the right thing...sorry about that.fs/proc/proc_sysctl.c | 6 +++--- include/linux/string.h | 1 + mm/util.c | 27 +++++++++++++++++++++++++++ 3 files changed, 31 insertions(+), 3 deletions(-)fs/proc/proc_sysctl.c +0
-0fs/proc/proc_sysctl.c static ssize_t proc_sys_call_handler(struct file *filp, void __user *ubuf, · line 573
Quoted 4 lines
fs/proc/proc_sysctl.c +1
-1@@ -571,13 +571,13 @@ static ssize_t proc_sys_call_handler(struct file *filp, void __user *ubuf, goto out;if (write) { - kbuf =memdup_user_nul (ubuf, count); + kbuf =vmemdup_user_nul (ubuf, count);Given that this can also do a kmalloc and thus needs to be paired with kvfree shouldn't it be
kvmemdup_user_nul?Quoted 5 lines
Quoted 2 lines
There's an existing
vmemdup_userthat does kvmalloc, so I followed the existing naming convention. Do you want me to change them both? Thanks,I personally would, and given that it only has a few users it might even be feasible.
FWIW, how about following or combining that with "allocate count + 1 bytes on the read side"? Allows some nice cleanups - e.g.
len = sprintf(tmpbuf, "0x%04x", *(unsigned int *) table->data); if (len > left) len = left; memcpy(buffer, tmpbuf, len); if ((left -= len) > 0) { *((char *)buffer + len) = '\n'; left--; } in sunrpc proc_dodebug() turns into left -= snprintf(buffer, left, "0x%04x\n", *(unsigned int *) table->data);
Quoted 2 lines
and that's not the only example.
We wouldn't even need the extra +1 part, since we're only copying in how much the user wants anyway, we could just go ahead and convert this to
left -= snprintf(buffer, left, "0x%04x\n", *(unsigned int *) table->data);
and be fine, right? Or am I misunderstanding what you're looking for? Thanks,
Josef
Al Viro
maintainerQuoted 2 lines · Josef Bacik
Quoted 2 lines
in sunrpc
proc_dodebug()turns intoleft -= snprintf(buffer, left, "0x%04x\n",
^^^^ left + 1, that is.
Quoted 10 lines
Quoted 3 lines
*(unsigned int *) table->data);and that's not the only example.
We wouldn't even need the extra +1 part, since we're only copying in how much the user wants anyway, we could just go ahead and convert this to
left -=
snprintf(buffer, left, "0x%04x\n", *(unsigned int *) table->data);and be fine, right? Or am I misunderstanding what you're looking for? Thanks,
snprintf() always produces a NUL-terminated string. And if you are passing 7 as len, you want 0xf0ad\n to be copied to user. For that you need 8 passed to snprintf, and 8-byte buffer given to it.
Quoted 14 lines · Al Viro
Quoted 2 lines · Josef Bacik
Quoted 2 lines
in sunrpc
proc_dodebug()turns intoleft -= snprintf(buffer, left, "0x%04x\n",^^^^ left + 1, that is.Quoted 7 lines
Quoted 3 lines
*(unsigned int *) table->data);and that's not the only example.
We wouldn't even need the extra +1 part, since we're only copying in how much the user wants anyway, we could just go ahead and convert this to
Quoted 8 lines
Quoted 3 lines
left -=
snprintf(buffer, left, "0x%04x\n", *(unsigned int *) table->data);and be fine, right? Or am I misunderstanding what you're looking for? Thanks,
snprintf()always produces a NUL-terminated string. And if you are passing 7 as len, you want 0xf0ad\n to be copied to user. For that you need 8 passed to snprintf, and 8-byte buffer given to it.
Right, gotcha. I'll rig that up and see how it looks. I'd recommend looking through what I do with a fine tooth comb, I'm obviously not batting 1000 today. Thanks,
Josef
From: Josef Bacik
Quoted 1 line
Sent: 13 August 2020 18:19
...
Quoted 6 lines
We wouldn't even need the extra +1 part, since we're only copying in how much the user wants anyway, we could just go ahead and convert this to
left -=
snprintf(buffer, left, "0x%04x\n", *(unsigned int *) table->data);and be fine, right? Or am I misunderstanding what you're looking for? Thanks,
Doesn't that need to be scnprintf()?
IIRC snprintf() returns the number of bytes that would have been written were the buffer infinite size?
(I suspect this is an 'accidental' return value from the original SYSV? userspace implementation that just dumped characters that wouldn't fit in the buffer somewhere.)
David
-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)
Quoted 8 lines · David Laight
From: Josef Bacik
Quoted 1 line
Sent: 13 August 2020 18:19
...
Quoted 5 lines
We wouldn't even need the extra +1 part, since we're only copying in how much the user wants anyway, we could just go ahead and convert this to
left -=
snprintf(buffer, left, "0x%04x\n", *(unsigned int *) table->data);
Quoted 9 lines
Quoted 1 line
and be fine, right? Or am I misunderstanding what you're looking for? Thanks,
Doesn't that need to be
scnprintf()?
IIRCsnprintf()returns the number of bytes that would have been written were the buffer infinite size?
(I suspect this is an 'accidental' return value from the original SYSV? userspace implementation that just dumped characters that wouldn't fit in the buffer somewhere.)
Yeah, if you look at the patches I just sent you'll notice I used scnprintf() everywhere. Thanks,
Josef
From: Josef Bacik
Quoted 10 lines
Sent: 13 August 2020 15:53
sysctl: pass kernel pointers to ->proc_handler
we have been pre-allocating a buffer to copy the data from the proc handlers into, and then copying that to userspace. The problem is this just blind kmalloc()'s the buffer size passed in from the read, which in the case of our 'cat' binary was 64kib. Order-4 allocations are not awesome, and since we can potentially allocate up to our maximum order, use vmalloc for these buffers.
What happens if I run 'dd bs=16M ...' ?
David
-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)
Al Viro
maintainerQuoted 5 lines · David Laight
From: Josef Bacik
Quoted 4 lines
Sent: 13 August 2020 15:53
sysctl: pass kernel pointers to ->proc_handler
Quoted 8 lines
Quoted 6 lines
we have been pre-allocating a buffer to copy the data from the proc handlers into, and then copying that to userspace. The problem is this just blind kmalloc()'s the buffer size passed in from the read, which in the case of our 'cat' binary was 64kib. Order-4 allocations are not awesome, and since we can potentially allocate up to our maximum order, use vmalloc for these buffers.
What happens if I run 'dd bs=16M ...' ?
Try it.
Quoted 12 lines · David Laight
From: Josef Bacik
Quoted 10 lines
Sent: 13 August 2020 15:53
sysctl: pass kernel pointers to ->proc_handler
we have been pre-allocating a buffer to copy the data from the proc handlers into, and then copying that to userspace. The problem is this just blind kmalloc()'s the buffer size passed in from the read, which in the case of our 'cat' binary was 64kib. Order-4 allocations are not awesome, and since we can potentially allocate up to our maximum order, use vmalloc for these buffers.
Quoted 4 lines
What happens if I run 'dd bs=16M ...' ?
David
/* don't even try if the size is too large */
error = -ENOMEM;
if (count >= KMALLOC_MAX_SIZE)
goto out;is above this code, thanks,
Josef