From mboxrd@z Thu Jan 1 00:00:00 1970 Message-ID: <6133344834e8d41d2d18b937d847d482.axp-list@archive.invalid> X-Archive-Original-Message-ID: <1.5.4.32.19961018030033.006feee8@tiger.avana.net> Date: Fri, 18 Oct 1996 08:16:39 +0300 (EET DST) From: Linus Torvalds Subject: Re: Linux-2.1.4 In-Reply-To: <199610172203.RAA00640@atheist.tamu.edu> On Thu, 17 Oct 1996, Richard Henderson wrote: > > > There is still issue (tought not directly related to kernel source). What > > to do with such functions when the exception occurs in the middle of > > user space cleared, copyed from or copyed to (assuming functions as > > clear_user, strncpy_from_user, strncpy_to_user) ? > > The copy_to_user and copy_from_user functions Linus defined to return > the number of bytes _not_ copied. In doing the others for the Alpha, > I defined clear_user the same way. Right, this was what I had in mind. > As for strncpy_from_user, which Linus just mentioned but did not > define, I did something slightly different due to the difference in > the way the function would be used in the first place: it returnes > -EFAULT on exception, -ENAMETOOLONG if the buffer filled before the > terminator was copied (but it does always make sure that the kernel > buffer is terminated!) Otherwise, it returns the length of the string. There is one subtle issue with -ENAMETOOLONG.. In fact, you do NOT want to zero-terminate the string, and in fact you may not even want to return an error for this case. Your approach is the logical one, especially when you look at "get_name()", but there are some other uses for this function that make another return value a better choice. The thing I had in mind for strncpy_from_user() is not only for get_name(), but actually even more for another timing-critical function, namely "copy_strings()" in fs/exec.c. That function is used to copy all the argument and environment variables at execve() time, and that function is a noticeable part of the overhead of execve(). Now, "copy_strings()" does not have any upper limit on the string length (apart from a total limit of 32 pages worth of data: that's 256kB worth of data. Now, imagine a 256kB string.. It should work, but we _obviously_ do not want to copy it in one go, and obviously we don't. Right now we look up the length of the string (with a byte-at-a-time algorithm, UGH), and then we copy it into the kernel page buffers (again one byte at a time, double-UGH). I had thought we could do this with "strncpy_from_user()" and then just do do the proper memcpy()'s to move the _partial_ strings into the right place. Note the _partial_: we'd do string copies in parts. So if the string is larger than the buffer, we do _not_ want to zero-terminate the string, because we'll go back and copy the rest of the string into another kernel buffer until we hit the end.. So I suggest that "strncpy_from_user()" do something like this: - exception partway through: -EFAULT (and a partial string copy, but we don't care, we'll throw the string away anyway). - successful copy: number of bytes copied (length of string): 0 - (n-1) - partial copy, no zero-terminate: number of bytes copied: (n) With this approach, get_name() would become something like this: int retval; unsigned long page = __get_free_page(GFP_KERNEL); retval = -ENOMEM; if (page) { retval = strncpy_from_user((char *) page, ubuf, PAGE_SIZE); if (retval >= 0) { if (retval < PAGE_SIZE) { *namep = (char *) page; return 0; } retval = -ENAMETOOLONG; } free_page(page); } return retval; and the execve() set of functions should also be able to use the same strncpy_from_user() efficiently. NOTE! I'm not sure that this is necessarily the right approach. For various reasons it may be that for execve() we'd be better off with a "strlen_from_user()" + "memcpy_from_user()", because we have to know the length anyway before we know where to copy the string. I have no strong opinion one way or the other on this, I just know that we definitely do not want to do it one byte at a time. Especially not on an alpha. > Oh, and strncpy_to_user is not likely to be useful, as we usually > already know the length of the string and so copy_to_user is better. Indeed. Linus -- To unsubscribe: mail -s unsubscribe axp-list-request@redhat.com < /dev/null