Fix prctl(PR_GET_NAME) to not leak random trailing bytes
Helge Deller
maintainerPATCH 1/1 Fix prctl(PR_GET_NAME) to not leak random trailing bytes
The prctl(PR_GET_NAME) and prctl(PR_SET_NAME) syscalls are used to set and retrieve the process name. Those kernel functions are currently implemented to always copy the full array of 16-bytes back and forth between kernel and userspace instead of just copying the relevant bytes of the string.
This patch changes the prctl(PR_GET_NAME) to only copy back the null-terminated string (with max. up to 16 chars including the trailing zero) to userspace and thus avoids copying and leaking random trailing chars behind the process name.
Background:
The newest glibc testsuite includes a test which is implemented similiar to this:
prctl(PR_SET_NAME, "thread name", 0, 0, 0);
char buffer[16] = { 0, };
prctl(PR_GET_NAME, buffer, 0, 0, 0);
char expected[16] = "thread name";
fail if memcmp(buffer, expected, 16) != 0;
The compiler may put the "thread name" string given in the PR_SET_NAME call somewhere into memory and it's not guaranteed that trailing (up to a total of
16) chars behind that string has zeroes.
As such on the parisc architecture I've seen that the buffer[] array gets filled on return of prctl(PR_GET_NAME) with such additional random bytes, e.g.:
"thread name\000@\032i\000"
74 68 72 65 61 64 20 6E 61 6D 65 00 40 1A 69 00Unfortunatly the glibc testuite tests the full memory block of 16 bytes and fails because it expects zeroed characters behind the process name.
In addition to fix the glibc testsuite, I suggest to fix the kernel function of prctl(PR_GET_NAME) to just return the null-terminated process name.
kernel/sys.c +1 -1
@@ -2367,7 +2367,7 @@ SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,break ;case PR_GET_NAME :get_task_comm (comm, me); -if (copy_to_user ((char __user *)arg2, comm,sizeof (comm))) +if (copy_to_user ((char __user *)arg2, comm,strlen (comm) +1 ))return -EFAULT ;break ;case PR_GET_ENDIAN :
Quoted 40 lines · Helge Deller
The
prctl(PR_GET_NAME)andprctl(PR_SET_NAME)syscalls are used to set and retrieve the process name. Those kernel functions are currently implemented to always copy the full array of 16-bytes back and forth between kernel and userspace instead of just copying the relevant bytes of the string.This patch changes the
prctl(PR_GET_NAME)to only copy back the null-terminated string (with max. up to 16 chars including the trailing zero) to userspace and thus avoids copying and leaking random trailing chars behind the process name.Background:
The newest glibc testsuite includes a test which is implemented similiar to this:prctl(PR_SET_NAME, "thread name", 0, 0, 0); char buffer[16] = { 0, }; prctl(PR_GET_NAME, buffer, 0, 0, 0); char expected[16] = "thread name"; fail if memcmp(buffer, expected, 16) != 0;The compiler may put the "thread name" string given in the
PR_SET_NAMEcall somewhere into memory and it's not guaranteed that trailing (up to a total of
16) chars behind that string has zeroes.
As such on the parisc architecture I've seen that the buffer[] array gets filled on return ofprctl(PR_GET_NAME)with such additional random bytes, e.g.:"thread name\000@\032i\000" 74 68 72 65 61 64 20 6E 61 6D 65 00 40 1A 69 00Unfortunatly the glibc testuite tests the full memory block of 16 bytes and fails because it expects zeroed characters behind the process name.
In addition to fix the glibc testsuite, I suggest to fix the kernel function of
prctl(PR_GET_NAME)to just return the null-terminated process name.Signed-off-by Helge Deller <[email protected]>kernel/sys.c +0
-0
kernel/sys.c SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3, · line 2372
Quoted 7 lines
kernel/sys.c +1
-1@@ -2367,7 +2367,7 @@ SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,break ;case PR_GET_NAME :get_task_comm (comm, me); -if (copy_to_user ((char __user *)arg2, comm,sizeof (comm))) +if (copy_to_user ((char __user *)arg2, comm,strlen (comm) +1 ))return -EFAULT ;break ;
I don't understand. get_task_comm() is
extern char *__get_task_comm(char *to, size_t len, struct task_struct *tsk);
#define get_task_comm(buf, tsk) ({ \
BUILD_BUG_ON(sizeof(buf) != TASK_COMM_LEN); \
__get_task_comm(buf, sizeof(buf), tsk); \})
and __get_task_comm() is
char *__get_task_comm(char *buf, size_t buf_size, struct task_struct *tsk)
{
task_lock(tsk);
strncpy(buf, tsk->comm, buf_size);
task_unlock(tsk);
return buf;
}so the strncpy should ensure that the caller's buffer after the string's terminator gets zero-filled. I can see that parisc has its own strncpy(), but I can't read that asm, so I can't see if it actually does that mandated-by-C-standard zero-filling. It would surprise me if it didn't (I'd expect lots of other breakage), but OTOH it is the only way I can explain what you've seen.
[Also, the compiler most likely puts the "thread name" string literal first into a .rodata.strX.Y section, only later to be merged into .rodata by the linker, so anything after "thread name" I'd expect to also be some readable string. The fact that you have a 0x1a byte in there suggests that the garbage doesn't actually come from the original PR_SET_NAME call, but is consistent with it being a stack leak from the kernel. You could try running hexdump on the test binary to see if there's any occurrence of "thread name" followed by those particular garbage bytes.]
Rasmus
Helge Deller
maintainerQuoted 67 lines · Rasmus Villemoes
Quoted 40 lines · Helge Deller
The
prctl(PR_GET_NAME)andprctl(PR_SET_NAME)syscalls are used to set and retrieve the process name. Those kernel functions are currently implemented to always copy the full array of 16-bytes back and forth between kernel and userspace instead of just copying the relevant bytes of the string.This patch changes the
prctl(PR_GET_NAME)to only copy back the null-terminated string (with max. up to 16 chars including the trailing zero) to userspace and thus avoids copying and leaking random trailing chars behind the process name.Background:
The newest glibc testsuite includes a test which is implemented similiar to this:prctl(PR_SET_NAME, "thread name", 0, 0, 0); char buffer[16] = { 0, }; prctl(PR_GET_NAME, buffer, 0, 0, 0); char expected[16] = "thread name"; fail if memcmp(buffer, expected, 16) != 0;The compiler may put the "thread name" string given in the
PR_SET_NAMEcall somewhere into memory and it's not guaranteed that trailing (up to a total of
16) chars behind that string has zeroes.
As such on the parisc architecture I've seen that the buffer[] array gets filled on return ofprctl(PR_GET_NAME)with such additional random bytes, e.g.:"thread name\000@\032i\000" 74 68 72 65 61 64 20 6E 61 6D 65 00 40 1A 69 00Unfortunatly the glibc testuite tests the full memory block of 16 bytes and fails because it expects zeroed characters behind the process name.
In addition to fix the glibc testsuite, I suggest to fix the kernel function of
prctl(PR_GET_NAME)to just return the null-terminated process name.Signed-off-by Helge Deller <[email protected]>kernel/sys.c +0
-0kernel/sys.c SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3, · line 2372
Quoted 7 lines
kernel/sys.c +1
-1@@ -2367,7 +2367,7 @@ SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,break ;case PR_GET_NAME :get_task_comm (comm, me); -if (copy_to_user ((char __user *)arg2, comm,sizeof (comm))) +if (copy_to_user ((char __user *)arg2, comm,strlen (comm) +1 ))return -EFAULT ;break ;I don't understand.
get_task_comm()isextern char *__get_task_comm(char *to,
size_tlen, structtask_struct*tsk);#define get_task_comm(buf, tsk) ({ \ BUILD_BUG_ON(sizeof(buf) != TASK_COMM_LEN); \ __get_task_comm(buf, sizeof(buf), tsk); \ })and
__get_task_comm()ischar *__get_task_comm(char *buf,
size_tbuf_size, structtask_struct*tsk){ task_lock(tsk); strncpy(buf, tsk->comm, buf_size); task_unlock(tsk); return buf; }
Quoted 4 lines
so the strncpy should ensure that the caller's buffer after the string's terminator gets zero-filled. I can see that parisc has its own
strncpy(), but I can't read that asm, so I can't see if it actually does that mandated-by-C-standard zero-filling.
Oh, the parisc strncpy() asm does NOT zero-fill the target address !! That's the bug.
I thought strncpy would just copy up to given number of chars.
Quoted 3 lines
It would surprise me if it
didn't (I'd expect lots of other breakage), but OTOH it is the only way I can explain what you've seen.
Interestingly the kernel runs quite well and we don't see any bigger breakage. Anyway, the function needs fixing.
Please ignore this patch and thanks for pointing to the real bug.
Helge
Quoted 4 lines · Helge Deller
Oh, the parisc
strncpy()asm does NOT zero-fill the target address !! That's the bug.
I thought strncpy would just copy up to given number of chars.
Yeah, strncpy() is a horrible horrible function.
It doesn't copy any NUL character at all if the source length was longer than the 'n', so it doesn't necessarily result in a NUL-terminated string.
And it fills the target with NUL characters at the end, which is usually horribly inefficient and means that you should never ever use it with big destination buffers. So it can be a surprisingly bad function to use for the - not all that unusual situation - where you have lots of space in the destination buffer, and use the 'n' version of strcpy() just to be safe for odd situations. It will zero-fill all that space, usually for no good reason.
Very different from the other 'n' functions like snprintf() and friends that people use for that same "destination buffer safety" reason.
So it's almost never the right thing to use, even if it's the most traditional, most common and - by name - most obvious "copy string of at most length 'n'" function.
It so happens that "comm[]" is probably one of *very* few situations in the kernel where we really do want to use strncpy(), and where we don't just NUL-terminate, but NUL-fill the buffer.
Of course, that "comm[]" behavior is unusual these days, but I think it was a lot more usual back in the early 70's, when that whole "small, fixed-size string buffer" model was much much more common than it is today.
It is, after all, the exact same reason why the C language linker rules for identifiers were historically "only the first 7 letters are necessarily significant". Because "use a fixed 8-byte buffer for a string" made sense at the time in ways it doesn't necessarily make all that much sense today.
So that odd and nasty behavior of strncpy() makes a lot more sense in the historical context - it's just that that context is 50 years ago.
While mentioning all the oddities of 'strncpy()', it's also worth noting that despite the similarities in the name, "strncpy_from_user()" does *not* fill the end of the destination buffer with NUL characters, and does *not* act like strncpy(). The user string copy function obviously also has a very very different return value, which hopefully makes it more obvious that it's a very different beast.
Most of the time, if you actually want to copy a string, and have a limited destination buffer, you should use 'strscpy()'. Of the "limited size string" routines, it's pretty much the only sane one, and it guarantees - as long as the target size is non-zero - that the target is NUL-terminated without doing the NUL filling.
(The BSD 'strlcpy()' is horribly broken because the return value semantics means that it will have to find the terminating NUL of the *source* string, even if the source string is horribly long, or untrusted and unterminated).
Quoted 1 line
Interestingly the kernel runs quite well and we don't see any bigger breakage.
Yeah, almost nothing actually cares about the odd NUL filling that - as you - few people realize is even part of strncpy().
Quoted 1 line
Anyway, the function needs fixing.
I'd suggest you just use the default one in lib/string.c and not override it with __HAVE_ARCH_STRNCPY.
For example, on x86, we for purely historical reasons do that __HAVE_ARCH_STRNCPY thing, but only for the legacy 32-bit code. x86-64 uses that generic lib/string.c version (and honestly, the 32-bit arch-specific x86 one is almost certainly much worse, but hey, it really exists purely due to hysterical raisins).
That generic thing is a slow byte-per-byte implementation, but I don't think we have ever had a situation where it really matters.
It's also slightly oddly implemented - notice how the way it does that zero fill is by simply not incrementing the source pointer once it finds a NUL character. Simple and effective (even if perhaps not _efficient_), but it can make people miss what is going on because it's an unusual pattern.
Linus
Helge Deller
maintainer* Linus Torvalds <[email protected]>:
Quoted 60 lines · Helge Deller
Quoted 4 lines
Oh, the parisc
strncpy()asm does NOT zero-fill the target address !! That's the bug.
I thought strncpy would just copy up to given number of chars.Yeah,
strncpy()is a horrible horrible function.It doesn't copy any NUL character at all if the source length was longer than the 'n', so it doesn't necessarily result in a NUL-terminated string.
And it fills the target with NUL characters at the end, which is usually horribly inefficient and means that you should never ever use it with big destination buffers. So it can be a surprisingly bad function to use for the - not all that unusual situation - where you have lots of space in the destination buffer, and use the 'n' version of
strcpy()just to be safe for odd situations. It will zero-fill all that space, usually for no good reason.Very different from the other 'n' functions like
snprintf()and friends that people use for that same "destination buffer safety" reason.So it's almost never the right thing to use, even if it's the most traditional, most common and - by name - most obvious "copy string of at most length 'n'" function.
It so happens that "comm[]" is probably one of *very* few situations in the kernel where we really do want to use
strncpy(), and where we don't just NUL-terminate, but NUL-fill the buffer.Of course, that "comm[]" behavior is unusual these days, but I think it was a lot more usual back in the early 70's, when that whole "small, fixed-size string buffer" model was much much more common than it is today.
It is, after all, the exact same reason why the C language linker rules for identifiers were historically "only the first 7 letters are necessarily significant". Because "use a fixed 8-byte buffer for a string" made sense at the time in ways it doesn't necessarily make all that much sense today.
So that odd and nasty behavior of
strncpy()makes a lot more sense in the historical context - it's just that that context is 50 years ago.While mentioning all the oddities of '
strncpy()', it's also worth noting that despite the similarities in the name, "strncpy_from_user()" does *not* fill the end of the destination buffer with NUL characters, and does *not* act likestrncpy(). The user string copy function obviously also has a very very different return value, which hopefully makes it more obvious that it's a very different beast.Most of the time, if you actually want to copy a string, and have a limited destination buffer, you should use '
strscpy()'. Of the "limited size string" routines, it's pretty much the only sane one, and it guarantees - as long as the target size is non-zero - that the target is NUL-terminated without doing the NUL filling.
Quoted 4 lines
(The BSD '
strlcpy()' is horribly broken because the return value semantics means that it will have to find the terminating NUL of the *source* string, even if the source string is horribly long, or untrusted and unterminated).
Thanks for the explanations!
Quoted 9 lines
Quoted 1 line
Interestingly the kernel runs quite well and we don't see any bigger breakage.
Yeah, almost nothing actually cares about the odd NUL filling that - as you - few people realize is even part of
strncpy().Quoted 1 line
Anyway, the function needs fixing.
I'd suggest you just use the default one in
lib/string.cand not override it with__HAVE_ARCH_STRNCPY.
Yes, that's probably the best idea.
Would you be willing to apply the attached patch for v5.14 ?
It reverts the original commit in which I introduced the assembly str*() functions and adjusts the SPDX header tag. It has been in my for-next tree since yesterday.
Thanks,
Helge
--
From: Helge Deller <[email protected]>
Date: Fri, 27 Aug 2021 20:42:57 +0200
Subject: [PATCH] Revert "parisc: Add assembly implementations for memset,
strlen, strcpy, strncpy and strcat"
This reverts commit 83af58f8068ea3f7b3c537c37a30887bfa585069.
It turns out that at least the assembly implementation for strncpy() was buggy. Revert the whole commit and return back to the default coding.
arch/parisc/include/asm/string.h +0 -15
@@ -8,19 +8,4 @@ extern void * memset(void *, int, size_t);#define __HAVE_ARCH_MEMCPYvoid *memcpy (void * dest,const void *src,size_t count); -#define __HAVE_ARCH_STRLEN -extern size_t strlen (const char *s); - -#define __HAVE_ARCH_STRCPY -extern char *strcpy (char *dest,const char *src); - -#define __HAVE_ARCH_STRNCPY -extern char *strncpy (char *dest,const char *src,size_t count); - -#define __HAVE_ARCH_STRCAT -extern char *strcat (char *dest,const char *src); - -#define __HAVE_ARCH_MEMSET -extern void *memset (void *,int ,size_t ); -#endif
arch/parisc/kernel/parisc_ksyms.c +0 -4
@@ -17,10 +17,6 @@#include <linux/string.h> EXPORT_SYMBOL (memset); -EXPORT_SYMBOL (strlen); -EXPORT_SYMBOL (strcpy); -EXPORT_SYMBOL (strncpy); -EXPORT_SYMBOL (strcat);#include <linux/atomic.h> EXPORT_SYMBOL (__xchg8);
arch/parisc/lib/Makefile +2 -2
@@ -3,7 +3,7 @@ # Makefile for parisc-specific library files # -lib-y := lusercopy.o bitops.o checksum.o io.o memcpy.o \ - ucmpdi2.o delay.o string.o +lib-y := lusercopy.o bitops.o checksum.o io.o memset.o memcpy.o \ + ucmpdi2.o delay.o obj-y := iomap.o
arch/parisc/lib/memset.c new file +72 -0
72 lines not on this page · raw message
arch/parisc/lib/string.S deleted +0 -136
136 lines not on this page · raw message