All of lore.kernel.org
 help / color / mirror / Atom feed
From: walter harms <wharms@bfs.de>
To: kernel-janitors@vger.kernel.org
Subject: Re: [KJ] critical bug in strncpy()
Date: Mon, 28 Mar 2005 19:19:27 +0000	[thread overview]
Message-ID: <424858BF.9080707@bfs.de> (raw)
In-Reply-To: <200503281155.16199.vicente.feito@gmail.com>

[-- Attachment #1: Type: text/plain, Size: 3116 bytes --]



Ryan Anderson wrote:
> On Mon, 2005-03-28 at 17:31 +0200, walter harms wrote:
> 
>>hi all,
>>
>>i do not think this is intended. surely you copy data from
>>src to tmp++ but using a large value (> strlen(src) ) for count will 
>>access some strange areas where no code should go.
> 
> 
> It's not a strange area, it's the rest of the destination buffer.
> The rest of the destination buffer is filled with the value 0.  This
> guarantees that strncpy prevents any information leakage via this
> buffer.  This has the nice side effect that a buffer can be reused
> without an explicit memset if you know that strncpy will be used to
> repopulate it between uses.
> 
> This is a security feature.
> 
> src is not accessed beyond the minimum of strlen(src) and n, so there is
> no chance of an out of bounds access on that, and the "n" that we are
> given is the limit to the buffer pointed at by dest, so there is no out
> of bounds access there.
> 
> 
>>the defined behavier of strncpy is to copy a string that terminates with 
>>\0, count describes an upper limit what is reached first should 
>>terminate the copy. every version of strncpy works that way.
>>maybe its not an critical bug (as src is not increased)  but
>>1. it *will* access unintended areas beyond  \0
> 
> 
> src is not accessed beyond the minimum of strlen(src) and n, so there is
> no chance of an out of bounds access on that, and the "n" that we are
> given is the limit to the buffer pointed at by dest, so there is no out
> of bounds access there.
> 
> 
>>2. rule of least surprise is violated
> 
> 
> Well, I can't disagree here - but I don't think the surprise is in any
> way detrimental.  There is simply no bug to exploit here.
> 
> 
>>3. other ppl may find other uses to this bug
>>4. the fix is simple
> 
> 
> The fix opens a hole for potential information leakage to user space.
> 
> Have you audited all callers of strncpy to ensure that they do not
> implicitly use this clearing of the buffer to guarantee that information
> is not leaked to userspace?
> 

you are right *when* n is the size of the destination buffer.
but take a look at this time of code from 
./net/irda/ircomm/ircomm_param.c:
strncpy(self->settings.port_name, param->pv.c, 32);

if works fine IF port_name[33] is true. but then he can use strcpy(). 
why does user use actualy strNxxx() because the limit will prevent 
access beyond \0 and at least n. clearing should be done within strncpy 
(if you want that) using a clear *foo++=0 so its visible

look at the asm code for m68k (i can not read i386, that the reason :)
they stop coppying at \0 and n.

the main problem with strncpy is still:
it has the same name as the POSIX cousin but it does not behave equal.
it does not terminate the dststring automaticly with \0 (neither does 
POSIX, an error IMHO,strlcpy does).
it will access the dstbuffer beyond \0 if n is large enought, ok it will 
fill it with \0 but who many people will be aware of it ?
i admit falling into a trap when expecting the same behavier as POSIX 
strncpy() but who will not ? exspecialy if its not documented.



re,
	walter







[-- Attachment #2: Type: text/plain, Size: 167 bytes --]

_______________________________________________
Kernel-janitors mailing list
Kernel-janitors@lists.osdl.org
http://lists.osdl.org/mailman/listinfo/kernel-janitors

  parent reply	other threads:[~2005-03-28 19:19 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-03-28 11:55 [KJ] critical bug in strncpy() Vicente Feito
2005-03-28 14:34 ` walter harms
2005-03-28 15:03 ` Ryan Anderson
2005-03-28 15:31 ` walter harms
2005-03-28 18:18 ` Ryan Anderson
2005-03-28 19:19 ` walter harms [this message]
2005-03-28 21:26 ` Matthew Wilcox

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=424858BF.9080707@bfs.de \
    --to=wharms@bfs.de \
    --cc=kernel-janitors@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.