Bug 6409 - (glibc 2.7) -D_FORTIFY_SOURCE discards qualifier overrides on {str,mem}cpy
Summary: (glibc 2.7) -D_FORTIFY_SOURCE discards qualifier overrides on {str,mem}cpy
Status: RESOLVED WORKSFORME
Alias: None
Product: glibc
Classification: Unclassified
Component: libc (show other bugs)
Version: unspecified
: P2 normal
Target Milestone: ---
Assignee: Ulrich Drepper
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2008-04-15 16:53 UTC by Kees Cook
Modified: 2014-07-03 11:38 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Last reconfirmed:
fweimer: security-


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Kees Cook 2008-04-15 16:53:23 UTC
The following source, without the "(void*)" overrides, will throw an warning
(as expected), when compiled with -Wall:

 $ gcc -o memcpy-fortify -Wall memcpy-fortify.c
 memcpy-fortify.c: In function 'main':
 memcpy-fortify.c:21: warning: passing argument 1 of 'memcpy' discards
qualifiers from pointer target type
 memcpy-fortify.c:22: warning: passing argument 1 of 'strcpy' discards
qualifiers from pointer target type

With "(void*)" it is (as expected) silent. With -O2, it is silent, but with
-D_FORTIFY_SOURCE != 0, the qualifier override is ignored:

 $ gcc -o memcpy-fortify -Wall -O2 -D_FORTIFY_SOURCE=2 memcpy-fortify.c
 memcpy-fortify.c: In function 'main':
 memcpy-fortify.c:21: warning: passing argument 1 of 'memcpy' discards
qualifiers from pointer target type
 memcpy-fortify.c:22: warning: passing argument 1 of 'strcpy' discards
qualifiers from pointer target type

This will cause problems for builds that run with -Werror.

/*
 * gcc -o memcpy-fortify -Wall -Werror -O2 -D_FORTIFY_SOURCE=2 memcpy-fortify.c
 *
 */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdint.h>
#include <string.h>
#include <stdint.h>
#include <inttypes.h>

int main(int argc, char * argv[])
{
    char *foo = strdup("string one");
    char *bar = strdup("string two");
    const char *baz = (const char *)foo;

    printf("%s\n", foo);

    memcpy((void*)baz, bar, strlen(bar)+1);
    strcpy((void*)baz, bar);

    printf("%s\n", foo);

    return 0;
}
Comment 1 Ulrich Drepper 2008-04-15 19:16:58 UTC
You're using code which is too old.  glibc 2.8 is out.
Comment 2 Kees Cook 2008-04-29 20:19:15 UTC
I don't see 2.8 listed here:
http://ftp.gnu.org/gnu/glibc/

Do you mean to say that 2.8 fixes this bug?  If so, do you have a pointer to the
commit that fixed it so I might try backporting it to the 2.7 release?

Thanks.