Util-Linux package development
 help / color / mirror / Atom feed
From: "Aurélien Aptel" <aaptel@suse.com>
To: Karel Zak <kzak@redhat.com>, Tobias Stoeckmann <tobias@stoeckmann.org>
Cc: util-linux@vger.kernel.org
Subject: Re: [PATCH] libblkid: Avoid strlen if only first char is checked
Date: Thu, 06 Oct 2016 15:22:03 +0200	[thread overview]
Message-ID: <mpstwcp3950.fsf@suse.com> (raw)
In-Reply-To: <20161006125912.p2xyff3h44gc36ub@ws.net.home>

Karel Zak <kzak@redhat.com> writes:
> On Mon, Oct 03, 2016 at 10:05:03PM +0200, Tobias Stoeckmann wrote:
>> A strlen() call can lead to out of boundary read access if the
>> superblock in question has no nul-bytes after the string. This
>> could be avoided by using strnlen() but the calls in question
>> merely existed to check if the string length is not 0.
>>=20
>> By changing the calls as proposed with this diff, these files are
>> in sync with other superblock files, which do exactly the same.
>> ---
>>  libblkid/src/superblocks/befs.c  | 2 +-
>>  libblkid/src/superblocks/ext.c   | 2 +-
>>  libblkid/src/superblocks/jfs.c   | 2 +-
>>  libblkid/src/superblocks/nilfs.c | 2 +-
>>  libblkid/src/superblocks/romfs.c | 2 +-
>>  libblkid/src/superblocks/xfs.c   | 2 +-
>>  6 files changed, 6 insertions(+), 6 deletions(-)
>
>  Applied, thanks.
>
>> diff --git a/libblkid/src/superblocks/befs.c b/libblkid/src/superblocks/=
befs.c
>> index 7e9eaf6..36e079f 100644
>> --- a/libblkid/src/superblocks/befs.c
>> +++ b/libblkid/src/superblocks/befs.c
>> @@ -451,7 +451,7 @@ static int probe_befs(blkid_probe pr, const struct b=
lkid_idmag *mag)
>>  	/*
>>  	 * all checks pass, set LABEL, VERSION and UUID
>>  	 */
>> -	if (strlen(bs->name))
>> +	if (*bs->name !=3D '\0')
>
> Good catch, I hate it too. BTW, you can use
>
>     if (*bs->name)
>
> it's enough.

Interesting to note that GCC compiles it both to the same instructions:

https://godbolt.org/g/adKv1I

#include <string.h>
int code1(const char *s) {
  if(strlen(s)) return 1;
  return 0;
}

int code2(const char *s) {
  if(*s) return 1;
  return 0;
}

code1(char const*):
        push    rbp
        mov     rbp, rsp
        mov     QWORD PTR [rbp-8], rdi
        mov     rax, QWORD PTR [rbp-8]
        movzx   eax, BYTE PTR [rax]
        test    al, al
        je      .L2
        mov     eax, 1
        jmp     .L3
.L2:
        mov     eax, 0
.L3:
        pop     rbp
        ret
code2(char const*):
        push    rbp
        mov     rbp, rsp
        mov     QWORD PTR [rbp-8], rdi
        mov     rax, QWORD PTR [rbp-8]
        movzx   eax, BYTE PTR [rax]
        test    al, al
        je      .L5
        mov     eax, 1
        jmp     .L6
.L5:
        mov     eax, 0
.L6:
        pop     rbp
        ret

--=20
Aur=C3=A9lien Aptel / SUSE Labs Samba Team
GPG: 1839 CB5F 9F5B FB9B AA97  8C99 03C8 A49B 521B D5D3
SUSE Linux GmbH, Maxfeldstra=C3=9Fe 5, 90409 N=C3=BCrnberg, Germany
GF: Felix Imend=C3=B6rffer, Jane Smithard, Graham Norton, HRB 21284 (AG N=
=C3=BCrnberg)

  reply	other threads:[~2016-10-06 13:22 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-10-03 20:05 [PATCH] libblkid: Avoid strlen if only first char is checked Tobias Stoeckmann
2016-10-06 12:59 ` Karel Zak
2016-10-06 13:22   ` Aurélien Aptel [this message]
2016-10-06 19:02   ` Bruce Dubbs

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=mpstwcp3950.fsf@suse.com \
    --to=aaptel@suse.com \
    --cc=kzak@redhat.com \
    --cc=tobias@stoeckmann.org \
    --cc=util-linux@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox