1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
| MODULE_LICENSE("GPL"); MODULE_AUTHOR("William Woodruff <william@yossarian.net>"); MODULE_DESCRIPTION("A Kernelspace Randomized Faulter");
static int krf_init(void); static void krf_teardown(void); static ssize_t rng_state_file_read(struct file *, char __user *, size_t, loff_t *); static ssize_t rng_state_file_write(struct file *, const char __user *, size_t, loff_t *); static ssize_t probability_file_read(struct file *, char __user *, size_t, loff_t *); static ssize_t probability_file_write(struct file *, const char __user *, size_t, loff_t *); static ssize_t control_file_write(struct file *, const char __user *, size_t, loff_t *); static ssize_t log_faults_file_read(struct file *, char __user *, size_t, loff_t *); static ssize_t log_faults_file_write(struct file *, const char __user *, size_t, loff_t *); static ssize_t targeting_file_read(struct file *, char __user *, size_t, loff_t *); static ssize_t targeting_file_write(struct file *, const char __user *, size_t, loff_t *); static struct proc_dir_entry *krf_dir;
static const struct file_operations rng_state_file_ops = { .owner = THIS_MODULE, .read = rng_state_file_read, .write = rng_state_file_write, };
static const struct file_operations probability_file_ops = { .owner = THIS_MODULE, .read = probability_file_read, .write = probability_file_write, };
static const struct file_operations control_file_ops = { .owner = THIS_MODULE, .write = control_file_write, };
static const struct file_operations log_faults_file_ops = { .owner = THIS_MODULE, .read = log_faults_file_read, .write = log_faults_file_write, };
static const struct file_operations targeting_file_ops = { .owner = THIS_MODULE, .read = targeting_file_read, .write = targeting_file_write, };
int init_module(void) { int ret; if ((ret = krf_init()) != 0) { printk(KERN_ERR "krf_init failed with %d\n", ret); return ret; } #ifdef KRF_CODEGEN #include "krf.gen.x" #endif printk(KERN_INFO "krf " KRF_VERSION " loaded\n"); return 0; }
void cleanup_module(void) { krf_teardown(); printk(KERN_INFO "krf " KRF_VERSION " unloaded\n"); }
static int krf_init(void) { if (setup_netlink_socket() < 0) { return -1; } linux_sys_call_table = (void *)kallsyms_lookup_name("sys_call_table"); if (linux_sys_call_table == NULL) { printk(KERN_ERR "krf couldn't load the syscall table\n"); return -2; } memcpy(krf_sys_call_table, linux_sys_call_table, KRF_NR_SYSCALLS * sizeof(unsigned long *)); krf_dir = proc_mkdir(KRF_PROC_DIR, NULL);
if (krf_dir == NULL) { printk(KERN_ERR "krf couldn't create /proc/" KRF_PROC_DIR); return -2; } if (proc_create(KRF_RNG_STATE_FILENAME, 644, krf_dir, &rng_state_file_ops) == NULL || proc_create(KRF_PROBABILITY_FILENAME, 644, krf_dir, &probability_file_ops) == NULL || proc_create(KRF_CONTROL_FILENAME, 644, krf_dir, &control_file_ops) == NULL || proc_create(KRF_LOG_FAULTS_FILENAME, 644, krf_dir, &log_faults_file_ops) == NULL || proc_create(KRF_TARGETING_FILENAME, 644, krf_dir, &targeting_file_ops) == NULL) { printk(KERN_ERR "krf couldn't create /proc entries\n"); return -3; }
return 0; }
static void krf_teardown(void) { krf_flush_table(); remove_proc_subtree(KRF_PROC_DIR, NULL); destroy_netlink_socket(); }
static ssize_t rng_state_file_read(struct file *f, char __user *ubuf, size_t size, loff_t *off) { char buf[KRF_PROCFS_MAX_SIZE + 1] = {0}; size_t buflen = 0; sprintf(buf, "%u\n", krf_rng_state); buflen = strnlen(buf, KRF_PROCFS_MAX_SIZE); if (*off > 0 || size < buflen) { return 0; } if (copy_to_user(ubuf, buf, buflen)) { return -EFAULT; } *off = buflen; return buflen; }
static ssize_t rng_state_file_write(struct file *f, const char __user *ubuf, size_t size, loff_t *off) { char buf[KRF_PROCFS_MAX_SIZE + 1] = {0}; size_t buflen = 0; if (size > KRF_PROCFS_MAX_SIZE) { size = KRF_PROCFS_MAX_SIZE; } if (copy_from_user(buf, ubuf, size)) { return -EFAULT; } if (kstrtouint(buf, 0, &krf_rng_state) < 0) { return -EINVAL; } buflen = strnlen(buf, KRF_PROCFS_MAX_SIZE); *off = buflen; return buflen; }
static ssize_t control_file_write(struct file *f, const char __user *ubuf, size_t size, loff_t *off) { char buf[KRF_PROCFS_MAX_SIZE + 1] = {0}; size_t buflen = 0; unsigned int sys_num = KRF_NR_SYSCALLS; if (size > KRF_PROCFS_MAX_SIZE) { size = KRF_PROCFS_MAX_SIZE; } if (copy_from_user(buf, ubuf, size)) { return -EFAULT; } if (kstrtouint(buf, 0, &sys_num) < 0) { return -EINVAL; } if (control_file_handler(sys_num) < 0) { return -EOPNOTSUPP; } buflen = strnlen(buf, KRF_PROCFS_MAX_SIZE); *off = buflen; return buflen; }
static ssize_t log_faults_file_read(struct file *f, char __user *ubuf, size_t size, loff_t *off) { char buf[KRF_PROCFS_MAX_SIZE + 1] = {0}; size_t buflen = 0; sprintf(buf, "%u\n", krf_log_faults); buflen = strnlen(buf, KRF_PROCFS_MAX_SIZE); if (*off > 0 || size < buflen) { return 0; } if (copy_to_user(ubuf, buf, buflen)) { return -EFAULT; } *off = buflen; return buflen; }
static ssize_t log_faults_file_write(struct file *f, const char __user *ubuf, size_t size, loff_t *off) { char buf[KRF_PROCFS_MAX_SIZE + 1] = {0}; size_t buflen = 0; if (size > KRF_PROCFS_MAX_SIZE) { size = KRF_PROCFS_MAX_SIZE; } if (copy_from_user(buf, ubuf, size)) { return -EFAULT; } if (kstrtouint(buf, 0, &krf_log_faults) < 0) { return -EINVAL; } krf_log_faults = !!krf_log_faults; buflen = strnlen(buf, KRF_PROCFS_MAX_SIZE); *off = buflen; return buflen; }
static ssize_t targeting_file_read(struct file *f, char __user *ubuf, size_t size, loff_t *off) { char buf[KRF_PROCFS_MAX_SIZE + 1] = {0}; size_t buflen = 0; targeting_file_read_handler(buf); buflen = strnlen(buf, KRF_PROCFS_MAX_SIZE); if (*off > 0 || size < buflen) { return 0; } if (copy_to_user(ubuf, buf, buflen)) { return -EFAULT; } *off = buflen; return buflen; }
static ssize_t targeting_file_write(struct file *f, const char __user *ubuf, size_t size, loff_t *off) { char buf[KRF_PROCFS_MAX_SIZE + 1] = {0}; size_t buflen = 0; krf_target_mode_t mode; unsigned int data; if (size > KRF_PROCFS_MAX_SIZE) { size = KRF_PROCFS_MAX_SIZE; } if (copy_from_user(buf, ubuf, size)) { return -EFAULT; } if (sscanf(buf, "%u %u", &mode, &data) != 2) { return -EINVAL; } if (targeting_file_write_handler(mode, data) < 0) { return -EINVAL; } buflen = strnlen(buf, KRF_PROCFS_MAX_SIZE); *off = buflen; return buflen; }
|