Red Hat axp-list: Linux on Alpha (1995-2022)
 help / color / mirror / Atom feed
From: Linus Torvalds <torvalds@cs.Helsinki.FI>
Subject: Re: Linux-2.1.4
Date: Fri, 18 Oct 1996 08:16:39 +0300 (EET DST)	[thread overview]
Message-ID: <6133344834e8d41d2d18b937d847d482.axp-list@archive.invalid> (raw)
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

  reply	other threads:[~1996-10-18  5:16 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <Pine.LNX.3.95.961017232056.6666A-100000@odin.asgard.hr>
1996-10-17 22:03 ` Linux-2.1.4 Richard Henderson
1996-10-18  5:16   ` Linus Torvalds [this message]
     [not found] <Pine.LNX.3.91.961018075100.23966E-100000@linux.cs.Helsinki.FI>
1996-10-18  7:34 ` Linux-2.1.4 Richard Henderson
     [not found] <Pine.LNX.3.93.961017124005.6940C-100000@pc5829.hil.siemens.at>
1996-10-17 20:26 ` Linux-2.1.4 Richard Henderson
     [not found] <Pine.LNX.3.91.961017130736.25546C-100000@linux.cs.Helsinki.FI>
1996-10-17 11:52 ` Linux-2.1.4 Ingo Molnar
1996-10-17 14:00 ` Linux-2.1.4 Linus Torvalds
1996-10-17 10:31 Linux-2.1.4 David A Rusling
     [not found] <Pine.LNX.3.93.961017104701.8482A-100000@pc5829.hil.siemens.at>
1996-10-17 10:18 ` Linux-2.1.4 Linus Torvalds
  -- strict thread matches above, loose matches on Subject: below --
1996-10-17 10:03 Linux-2.1.4 David A Rusling
     [not found] <Pine.LNX.3.91.961017082647.20618H-100000@linux.cs.Helsinki.FI>
1996-10-17 10:00 ` Linux-2.1.4 Ingo Molnar
     [not found] <199610171003.KAA00502@linux.reo.dec.com>
1996-10-17  9:03 ` Linux-2.1.4 Linus Torvalds
     [not found] <Pine.LNX.3.93.961016194825.19685A-100000@pc5829.hil.siemens.at>
1996-10-17  6:06 ` Linux-2.1.4 Linus Torvalds
     [not found] <Pine.LNX.3.91.961016101122.19371J-100000@linux.cs.Helsinki.FI>
1996-10-16 17:20 ` Linux-2.1.4 Theodore Y. Ts'o
1996-10-16 18:58   ` Linux-2.1.4 Ingo Molnar
1996-10-17  4:51   ` Linux-2.1.4 Linus Torvalds
1996-10-17  3:42 ` Linux-2.1.4 Marc Singer
1996-10-17 12:39   ` Linux-2.1.4 Jim Nance
     [not found] <9610160531.AA04330@feral.com>
1996-10-16  7:18 ` Linux-2.1.4 Linus Torvalds
     [not found] <Pine.LNX.3.95.961015224149.4897A-100000@hlermen.ffm.fgan.de>
1996-10-15 21:56 ` Linux-2.1.4 Hans Lermen
1996-10-15 18:41 Linux-2.1.4 Michael Stiller
1996-10-15 21:15 ` Linux-2.1.4 Hans Lermen
     [not found] <Pine.LNX.3.91.961010180237.1289A-100000@linux.cs.Helsinki.FI>
1996-10-15 17:14 ` Linux-2.1.4 Linus Torvalds
read-only archive

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox