| 1 |
/* { dg-do compile } */ |
| 2 |
/* { dg-options "-fanalyzer" } */ |
| 3 |
/* { dg-require-effective-target analyzer } */ |
| 4 |
|
| 5 |
#include <string.h> |
| 6 |
|
| 7 |
#include "test-uaccess.h" |
| 8 |
|
| 9 |
typedef unsigned char u8; |
| 10 |
typedef unsigned __INT16_TYPE__ u16; |
| 11 |
typedef unsigned __INT32_TYPE__ u32; |
| 12 |
|
| 13 |
struct s1 |
| 14 |
{ |
| 15 |
u32 i; |
| 16 |
}; |
| 17 |
|
| 18 |
void test_1a (void __user *dst, u32 a) |
| 19 |
{ |
| 20 |
struct s1 s; |
| 21 |
s.i = a; |
| 22 |
copy_to_user(dst, &s, sizeof (struct s1)); /* { dg-bogus "" } */ |
| 23 |
} |
| 24 |
|
| 25 |
void test_1b (void __user *dst, u32 a) |
| 26 |
{ |
| 27 |
struct s1 s; |
| 28 |
copy_to_user(dst, &s, sizeof (struct s1)); /* { dg-warning "potential exposure of sensitive information by copying uninitialized data from stack" "warning" } */ |
| 29 |
/* { dg-message "4 bytes are uninitialized" "note how much" { target *-*-* } .-1 } */ |
| 30 |
} |
| 31 |
|
| 32 |
void test_1c (void __user *dst, u32 a) |
| 33 |
{ |
| 34 |
struct s1 s; |
| 35 |
memset (&s, 0, sizeof (struct s1)); |
| 36 |
copy_to_user(dst, &s, sizeof (struct s1)); /* { dg-bogus "" } */ |
| 37 |
} |
| 38 |
|
| 39 |
void test_1d (void __user *dst, u32 a) |
| 40 |
{ |
| 41 |
struct s1 s = {0}; |
| 42 |
copy_to_user(dst, &s, sizeof (struct s1)); /* { dg-bogus "" } */ |
| 43 |
} |
| 44 |
|
| 45 |
struct s2 |
| 46 |
{ |
| 47 |
u32 i; |
| 48 |
u32 j; /* { dg-message "field 'j' is uninitialized \\(4 bytes\\)" } */ |
| 49 |
}; |
| 50 |
|
| 51 |
void test_2a (void __user *dst, u32 a) |
| 52 |
{ |
| 53 |
struct s2 s; /* { dg-message "region created on stack here" "where" } */ |
| 54 |
/* { dg-message "capacity: 8 bytes" "capacity" { target *-*-* } .-1 } */ |
| 55 |
s.i = a; |
| 56 |
copy_to_user(dst, &s, sizeof (struct s2)); /* { dg-warning "potential exposure of sensitive information by copying uninitialized data from stack" "warning" } */ |
| 57 |
/* { dg-message "4 bytes are uninitialized" "note how much" { target *-*-* } .-1 } */ |
| 58 |
} |
| 59 |
|
| 60 |
void test_2b (void __user *dst, u32 a) |
| 61 |
{ |
| 62 |
struct s2 s; |
| 63 |
s.i = a; |
| 64 |
/* Copy with wrong size (only part of s2). */ |
| 65 |
copy_to_user(dst, &s, sizeof (struct s1)); |
| 66 |
} |
| 67 |
|
| 68 |
void test_2d (void __user *dst, u32 a) |
| 69 |
{ |
| 70 |
struct s2 s = {0}; |
| 71 |
s.i = a; |
| 72 |
copy_to_user(dst, &s, sizeof (struct s2)); /* { dg-bogus "" } */ |
| 73 |
} |
| 74 |
|
| 75 |
struct empty {}; |
| 76 |
|
| 77 |
void test_empty (void __user *dst) |
| 78 |
{ |
| 79 |
struct empty e; |
| 80 |
copy_to_user(dst, &e, sizeof (struct empty)); |
| 81 |
} |
| 82 |
|
| 83 |
union un_a |
| 84 |
{ |
| 85 |
u32 i; |
| 86 |
u8 j; |
| 87 |
}; |
| 88 |
|
| 89 |
/* As above, but in a different order. */ |
| 90 |
|
| 91 |
union un_b |
| 92 |
{ |
| 93 |
u8 j; |
| 94 |
u32 i; |
| 95 |
}; |
| 96 |
|
| 97 |
void test_union_1a (void __user *dst, u8 v) |
| 98 |
{ |
| 99 |
union un_a u; /* { dg-message "region created on stack here" "where" } */ |
| 100 |
/* { dg-message "capacity: 4 bytes" "capacity" { target *-*-* } .-1 } */ |
| 101 |
u.j = v; |
| 102 |
copy_to_user(dst, &u, sizeof (union un_a)); /* { dg-warning "potential exposure of sensitive information by copying uninitialized data from stack" "warning" } */ |
| 103 |
/* { dg-message "3 bytes are uninitialized" "note how much" { target *-*-* } .-1 } */ |
| 104 |
/* { dg-message "bytes 1 - 3 are uninitialized" "note how much" { target *-*-* } .-2 } */ |
| 105 |
} |
| 106 |
|
| 107 |
void test_union_1b (void __user *dst, u8 v) |
| 108 |
{ |
| 109 |
union un_b u; /* { dg-message "region created on stack here" "where" } */ |
| 110 |
/* { dg-message "capacity: 4 bytes" "capacity" { target *-*-* } .-1 } */ |
| 111 |
u.j = v; |
| 112 |
copy_to_user(dst, &u, sizeof (union un_b)); /* { dg-warning "potential exposure of sensitive information by copying uninitialized data from stack" "warning" } */ |
| 113 |
/* { dg-message "3 bytes are uninitialized" "note how much" { target *-*-* } .-1 } */ |
| 114 |
/* { dg-message "bytes 1 - 3 are uninitialized" "note how much" { target *-*-* } .-2 } */ |
| 115 |
} |
| 116 |
|
| 117 |
void test_union_2a (void __user *dst, u8 v) |
| 118 |
{ |
| 119 |
union un_a u = {0}; |
| 120 |
u.j = v; |
| 121 |
copy_to_user(dst, &u, sizeof (union un_a)); |
| 122 |
} |
| 123 |
|
| 124 |
void test_union_2b (void __user *dst, u8 v) |
| 125 |
{ |
| 126 |
union un_b u = {0}; /* { dg-message "region created on stack here" "where" } */ |
| 127 |
/* { dg-message "capacity: 4 bytes" "capacity" { target *-*-* } .-1 } */ |
| 128 |
u.j = v; |
| 129 |
copy_to_user(dst, &u, sizeof (union un_b)); /* { dg-warning "potential exposure of sensitive information by copying uninitialized data from stack" "warning" } */ |
| 130 |
/* { dg-message "3 bytes are uninitialized" "note how much" { target *-*-* } .-1 } */ |
| 131 |
/* { dg-message "bytes 1 - 3 are uninitialized" "note how much" { target *-*-* } .-2 } */ |
| 132 |
} |
| 133 |
|
| 134 |
void test_union_3a (void __user *dst, u32 v) |
| 135 |
{ |
| 136 |
union un_a u; |
| 137 |
u.i = v; |
| 138 |
copy_to_user(dst, &u, sizeof (union un_a)); /* { dg-bogus "" } */ |
| 139 |
} |
| 140 |
|
| 141 |
void test_union_3b (void __user *dst, u32 v) |
| 142 |
{ |
| 143 |
union un_b u; |
| 144 |
u.i = v; |
| 145 |
copy_to_user(dst, &u, sizeof (union un_b)); /* { dg-bogus "" } */ |
| 146 |
} |
| 147 |
|
| 148 |
void test_union_4a (void __user *dst, u8 v) |
| 149 |
{ |
| 150 |
union un_a u = {0}; |
| 151 |
copy_to_user(dst, &u, sizeof (union un_a)); /* { dg-bogus "" } */ |
| 152 |
} |
| 153 |
|
| 154 |
void test_union_4b (void __user *dst, u8 v) |
| 155 |
{ |
| 156 |
union un_b u = {0}; /* { dg-message "region created on stack here" "where" } */ |
| 157 |
/* { dg-message "capacity: 4 bytes" "capacity" { target *-*-* } .-1 } */ |
| 158 |
copy_to_user(dst, &u, sizeof (union un_b)); /* { dg-warning "potential exposure of sensitive information by copying uninitialized data from stack" "warning" } */ |
| 159 |
/* { dg-message "3 bytes are uninitialized" "note how much" { target *-*-* } .-1 } */ |
| 160 |
/* { dg-message "bytes 1 - 3 are uninitialized" "note how much" { target *-*-* } .-2 } */ |
| 161 |
} |
| 162 |
|
| 163 |
struct st_union_5 |
| 164 |
{ |
| 165 |
union { |
| 166 |
u8 f1; |
| 167 |
u32 f2; |
| 168 |
} u; /* { dg-message "field 'u' is partially uninitialized" } */ |
| 169 |
}; |
| 170 |
|
| 171 |
void test_union_5 (void __user *dst, u8 v) |
| 172 |
{ |
| 173 |
struct st_union_5 st; /* { dg-message "region created on stack here" "where" } */ |
| 174 |
/* { dg-message "capacity: 4 bytes" "capacity" { target *-*-* } .-1 } */ |
| 175 |
|
| 176 |
/* This write only initializes the u8 within the union "u", |
| 177 |
leaving the remaining 3 bytes uninitialized. */ |
| 178 |
st.u.f1 = v; |
| 179 |
|
| 180 |
copy_to_user (dst, &st, sizeof(st)); /* { dg-warning "potential exposure of sensitive information by copying uninitialized data from stack" "warning" } */ |
| 181 |
/* { dg-message "3 bytes are uninitialized" "note how much" { target *-*-* } .-1 } */ |
| 182 |
} |
| 183 |
|
| 184 |
void test_one_byte (void __user *dst) |
| 185 |
{ |
| 186 |
char src; /* { dg-message "region created on stack here" "where" } */ |
| 187 |
/* { dg-message "capacity: 1 byte" "capacity" { target *-*-* } .-1 } */ |
| 188 |
|
| 189 |
copy_to_user (dst, &src, sizeof(src)); /* { dg-warning "potential exposure of sensitive information by copying uninitialized data from stack" "warning" } */ |
| 190 |
/* { dg-message "1 byte is uninitialized" "note how much" { target *-*-* } .-1 } */ |
| 191 |
} |