From mboxrd@z Thu Jan 1 00:00:00 1970 Message-ID: X-Archive-Original-Message-ID: <199610160504.WAA05277@netcom3.netcom.com> Date: Wed, 16 Oct 1996 08:33:51 +0300 (EET DST) From: Linus Torvalds Subject: Re: 2.1.4 exception question In-Reply-To: <199610160022.TAA24271@atheist.tamu.edu> On Tue, 15 Oct 1996, Richard Henderson wrote: > > I am aware that quite a bit was hashed about on linux-kernel recently, > but to clarify, I'm wondering about the implementation of read/write > as they exist in 2.1.4. I take as my example the routines from > drivers/char/mem.c. > > As it stands, copy_{to,from}_user correctly trap the exception, but > the surrounding code does nothing with the status returned from those > functions. Indeed. As I mentioned in my announcement notes, I've really only had time to do the low-level implementation, and as people have found out there are lots of places where the actual kernel code does not check the return values, and some code (notably the sound driver) that I didn't even try to change to use the new format yet. (Oh, and on the x86, the constraint should be "q" for the __copy_user() argument "(size & 3)", as people have already noticed on the kernel list, so I didn't even get all the low-level stuff correct ;) Now, the actual low-level routines are all hidden in essentially just one file per architecture (the file), but even so it took me five days to get it all to work the way I wanted. And I've been grumpy in the meantime, with more than a thousand mails in my mailbox during those days. Sorry to everybody I have been irritated at (Richard, David M-T, Alan, Pedro et al). Please accept my apologies. Your patches do seem to be the correct fix. And we definitely need to add the error condition checking code. But right now it would be more important to get the low-level code working exactly right, and do the RightThing (tm). There are a few things I'd like to get done on the low-level code before starting on the "trivial" stuff: - on alpha, do "get_user()" and "put_user()" without doing a function call. The function call has to load the function address, and also requires the (ugly) fixed registers. Instead of a function call, I'd _like_ to do something like this (this is just the 64-bit put_user case): __asm__ __inline__( "br $28,1f\n\t" /* set up exception address */ "lda %0,-14" /* exception: error = -EFAULT */ "br $31,2f\n" "1: stq %5,%3\n\t" /* store incremented exception count */ "stq %1,%2\n\t" /* actual quadword store */ "stq %4,%3\n" /* restore exception count */ "2:" :"=r" (result) :"r" (quad),"m" (*(long *)address), "m" (current->tss.ex.count), "r" (current->tss.ex.count), "r" (current->tss.ex.count+1)); - again, on alpha, the unaligned exception handler needs to recognize that a mm exception was pending, and use the exception handling "recursively" (easy to do as a special case) to make sure that an exception inside the unaligned exception handler is correctly handled down to the original code. That way we don't need to worry about unaligned accesses for the common case in get_user()/put_user(). - on x86, the "__copy_user()" macro needs to be optimized. It does some very stupid things for constant-sized copies (and those are actually pretty normal: copying small structures to user space). - in general, we need to do a "clear_user()" function that clears user space with exception handling. This is needed both in drivers/char/mem.c (read_zero()) and in the ELF loader (padzero()), both of which currently do a very stupid byte-at-a-time clear (and especially the ELF loader is actually somewhat timing critical). This should be pretty easy: just copy the "copy_to_user()" code but make it zero the area instead of copy. - we also need to do a "strncpy_from_user()" function that copies strings from user space - this is needed by execve() (argument handling) and by get_name() (filename and mount string handling). Both of these cases are performance-critical, and doing a "strncpy_from_user()" shouldn't be too bad. The above five things are the most important for me right now. Is there anybody who would like to look into this? There are other areas that need minor optimizations too, but the above five are the ones I _know_ have to be fixed in order to get the full performance advantage of the new scheme. > According to my understanding of the recent discussion, the appended > patch is required to make the code correct. Specifically, it actually > tests the return values and returns the amount actually written. Is > this indeed correct? Should this pattern be replicated to the other > drivers? These kinds of error checking patches should probably be put into all the code. NOTE! Adding error handling isn't always strictly needed. Even if you don't test the return code of the various user handling functions those functions will never do a questionable store. So the functions themselves are "safe" even in the absense of the final error checking. The only time we _have_ to do error checking is - the partial read or write impacts "meta-data" things (for example, in a file write we should check that we don't increment the file position the full length etc if we didn't actually write everything). - the partial read or write is a "secondary" security issue. For example, in the network layer a partial read from user space may mean that a packet is sent out with only partial data. That's generally fine (garbage in garbage out) BUT we must make sure that the _uncopied_ part of the packet doesn't contain any old data that may be a security hole (imagine the packet being allocated from the same physical page that previously contained secure information, and now only partially overwritten). > What about bits like those in, e.g., arch/alpha/kernel/osf_sys.c > osf_filldir() that similarly assume that the entire buffer is valid, > and blithely attempts to fill it all despite the fact that it > generates oodles of exceptions? This is one case where we might just ignore the exceptions. "oodles of exceptions" is not exactly what it should generate: you make it sound like there might be a performance impact. If a bad user program gives us a bad filldir() buffer,we might return a value that indicated that the filldir successfully completed, but obviously nothing ever actually got written into user space. That isn't our problem: that's the bad users problem. Blaah on him. > Or, on a slightly different tack, sys_newstat that never returns > an error. Or the set_termios bits which don't recognize that the > copy_from_user failed. True, POSIX says we needn't reliably report > such errors, but is this what _we_ want in this situation? We generally want to return -EFAULT. I added the code to do that in some trivial functions (kernel/time.c, for example: I used that file for some of my testing). But on the other hand we may not want to go out of our way to report the exceptions: if they aren't a kernel problem it's the users fault if he does stupid things. > Just wondering what the Official Position on these issues are > before doing wholesale changes. I'll be leaving for a week in the US this Sunday (househunting), and quite frankly I'd like to get the five "critical" points above fixed first. The "small but many" things (adding error checking) will probably have to wait until I get back. They are important too, but they aren't _fundamental_ important, if you get my drift. Linus -- To unsubscribe: mail -s unsubscribe axp-list-request@redhat.com < /dev/null