| 1 |
/* { dg-do compile } */ |
| 2 |
/* { dg-options "-fanalyzer" } */ |
| 3 |
/* { dg-require-effective-target analyzer } */ |
| 4 |
|
| 5 |
#include "test-uaccess.h" |
| 6 |
|
| 7 |
/* Adapted and simplified decls from linux kernel headers. */ |
| 8 |
|
| 9 |
typedef unsigned char u8; |
| 10 |
typedef unsigned __INT16_TYPE__ u16; |
| 11 |
typedef unsigned __INT32_TYPE__ u32; |
| 12 |
typedef signed __INT32_TYPE__ s32; |
| 13 |
typedef __SIZE_TYPE__ size_t; |
| 14 |
|
| 15 |
#define EFAULT 14 |
| 16 |
|
| 17 |
typedef unsigned int gfp_t; |
| 18 |
#define GFP_KERNEL 0 |
| 19 |
|
| 20 |
void kfree(const void *); |
| 21 |
void *kmalloc(size_t size, gfp_t flags) |
| 22 |
__attribute__((malloc (kfree))); |
| 23 |
|
| 24 |
/* Adapted from antipatterns.ko:taint.c (GPL-v2.0). */ |
| 25 |
|
| 26 |
struct cmd_1 |
| 27 |
{ |
| 28 |
u32 idx; |
| 29 |
u32 val; |
| 30 |
}; |
| 31 |
|
| 32 |
static u32 arr[16]; |
| 33 |
|
| 34 |
int taint_array_access(void __user *src) |
| 35 |
{ |
| 36 |
struct cmd_1 cmd; |
| 37 |
if (copy_from_user(&cmd, src, sizeof(cmd))) |
| 38 |
return -EFAULT; |
| 39 |
/* |
| 40 |
* cmd.idx is an unsanitized value from user-space, hence |
| 41 |
* this is an arbitrary kernel memory access. |
| 42 |
*/ |
| 43 |
arr[cmd.idx] = cmd.val; /* { dg-warning "use of attacker-controlled value 'cmd.idx' in array lookup without upper-bounds checking" } */ |
| 44 |
return 0; |
| 45 |
} |
| 46 |
|
| 47 |
struct cmd_2 |
| 48 |
{ |
| 49 |
s32 idx; |
| 50 |
u32 val; |
| 51 |
}; |
| 52 |
|
| 53 |
int taint_signed_array_access(void __user *src) |
| 54 |
{ |
| 55 |
struct cmd_2 cmd; |
| 56 |
if (copy_from_user(&cmd, src, sizeof(cmd))) |
| 57 |
return -EFAULT; |
| 58 |
if (cmd.idx >= 16) |
| 59 |
return -EFAULT; |
| 60 |
|
| 61 |
/* |
| 62 |
* cmd.idx hasn't been checked for being negative, hence |
| 63 |
* this is an arbitrary kernel memory access. |
| 64 |
*/ |
| 65 |
arr[cmd.idx] = cmd.val; /* { dg-warning "use of attacker-controlled value 'cmd.idx' in array lookup without checking for negative" } */ |
| 66 |
return 0; |
| 67 |
} |
| 68 |
|
| 69 |
struct cmd_s32_binop |
| 70 |
{ |
| 71 |
s32 a; |
| 72 |
s32 b; |
| 73 |
s32 result; |
| 74 |
}; |
| 75 |
|
| 76 |
int taint_divide_by_zero_direct(void __user *uptr) |
| 77 |
{ |
| 78 |
struct cmd_s32_binop cmd; |
| 79 |
if (copy_from_user(&cmd, uptr, sizeof(cmd))) |
| 80 |
return -EFAULT; |
| 81 |
|
| 82 |
/* cmd.b is attacker-controlled and could be zero */ |
| 83 |
cmd.result = cmd.a / cmd.b; /* { dg-warning "use of attacker-controlled value 'cmd.b' as divisor without checking for zero" } */ |
| 84 |
|
| 85 |
if (copy_to_user (uptr, &cmd, sizeof(cmd))) |
| 86 |
return -EFAULT; |
| 87 |
return 0; |
| 88 |
} |
| 89 |
|
| 90 |
int taint_divide_by_zero_compound(void __user *uptr) |
| 91 |
{ |
| 92 |
struct cmd_s32_binop cmd; |
| 93 |
if (copy_from_user(&cmd, uptr, sizeof(cmd))) |
| 94 |
return -EFAULT; |
| 95 |
|
| 96 |
/* |
| 97 |
* cmd.b is attacker-controlled and could be -1, hence |
| 98 |
* the divisor could be zero |
| 99 |
*/ |
| 100 |
cmd.result = cmd.a / (cmd.b + 1); /* { dg-warning "use of attacker-controlled value 'cmd.b \\+ 1' as divisor without checking for zero" } */ |
| 101 |
|
| 102 |
if (copy_to_user (uptr, &cmd, sizeof(cmd))) |
| 103 |
return -EFAULT; |
| 104 |
return 0; |
| 105 |
} |
| 106 |
|
| 107 |
int taint_mod_by_zero_direct(void __user *uptr) |
| 108 |
{ |
| 109 |
struct cmd_s32_binop cmd; |
| 110 |
if (copy_from_user(&cmd, uptr, sizeof(cmd))) |
| 111 |
return -EFAULT; |
| 112 |
|
| 113 |
/* cmd.b is attacker-controlled and could be zero */ |
| 114 |
cmd.result = cmd.a % cmd.b; /* { dg-warning "use of attacker-controlled value 'cmd.b' as divisor without checking for zero" } */ |
| 115 |
|
| 116 |
if (copy_to_user (uptr, &cmd, sizeof(cmd))) |
| 117 |
return -EFAULT; |
| 118 |
return 0; |
| 119 |
} |
| 120 |
|
| 121 |
int taint_mod_by_zero_compound(void __user *uptr) |
| 122 |
{ |
| 123 |
struct cmd_s32_binop cmd; |
| 124 |
if (copy_from_user(&cmd, uptr, sizeof(cmd))) |
| 125 |
return -EFAULT; |
| 126 |
|
| 127 |
/* |
| 128 |
* cmd.b is attacker-controlled and could be -1, hence |
| 129 |
* the divisor could be zero |
| 130 |
*/ |
| 131 |
cmd.result = cmd.a % (cmd.b + 1); /* { dg-warning "use of attacker-controlled value 'cmd.b \\+ 1' as divisor without checking for zero" } */ |
| 132 |
|
| 133 |
if (copy_to_user (uptr, &cmd, sizeof(cmd))) |
| 134 |
return -EFAULT; |
| 135 |
return 0; |
| 136 |
} |
| 137 |
|
| 138 |
/* TODO: etc. */ |