Alexey Kodanev | ddf49bf | 2013-12-04 13:27:31 +0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2013 Oracle and/or its affiliates. All Rights Reserved. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License as |
| 6 | * published by the Free Software Foundation; either version 2 of |
| 7 | * the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it would be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write the Free Software Foundation, |
| 16 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 | * |
| 18 | * Author: Alexey Kodanev <alexey.kodanev@oracle.com> |
| 19 | * |
| 20 | * Test checks kernel API to safely access user-space memory using |
| 21 | * copy_to_user(), copy_from_user(), get_user(), put_user() functions. |
| 22 | * |
| 23 | */ |
| 24 | |
| 25 | #include <stdio.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <unistd.h> |
| 28 | |
| 29 | #include "test.h" |
Alexey Kodanev | ddf49bf | 2013-12-04 13:27:31 +0400 | [diff] [blame] | 30 | #include "tst_module.h" |
| 31 | #include "safe_macros.h" |
| 32 | #include "safe_stdio.h" |
| 33 | |
| 34 | #include "ltp_uaccess.h" |
| 35 | |
| 36 | char *TCID = DEV_NAME; |
| 37 | |
| 38 | static const char dev_result[] = "/sys/devices/" DEV_NAME "/result"; |
| 39 | static const char dev_tcase[] = "/sys/devices/" DEV_NAME "/tcase"; |
| 40 | static const char module_name[] = DEV_NAME ".ko"; |
| 41 | static int module_loaded; |
| 42 | |
| 43 | static void cleanup(void) |
| 44 | { |
| 45 | if (module_loaded) |
| 46 | tst_module_unload(NULL, module_name); |
Alexey Kodanev | ddf49bf | 2013-12-04 13:27:31 +0400 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | static int set_ptr_to_sysfs(int id, const void *ptr, const char *descr) |
| 50 | { |
| 51 | int res; |
| 52 | tst_resm(TINFO, "TC %d: %s, ptr '%p'", id, descr, ptr); |
| 53 | SAFE_FILE_PRINTF(cleanup, dev_tcase, "%d %lu", id, (unsigned long) ptr); |
| 54 | SAFE_FILE_SCANF(cleanup, dev_result, "%d", &res); |
| 55 | if (res) |
| 56 | return TFAIL; |
| 57 | |
| 58 | return TPASS; |
| 59 | } |
| 60 | |
| 61 | /* |
| 62 | * Read user-space memory using copy_from_user(), get_user(). |
| 63 | */ |
| 64 | static void tc_read_userspace(void) |
| 65 | { |
| 66 | int res = set_ptr_to_sysfs(TC_READ_USER, test_str, |
| 67 | "read user-space memory from kernel"); |
| 68 | |
| 69 | tst_resm(res, "copy_from_user(), get_user(): strings%sequal", |
| 70 | (res) ? " not " : " "); |
| 71 | } |
| 72 | |
| 73 | /* |
| 74 | * Write from kernel-space to user-space |
| 75 | * using copy_to_user(), put_user(). |
| 76 | */ |
| 77 | static void tc_write_userspace(void) |
| 78 | { |
| 79 | char buf[str_size]; |
| 80 | memset(buf, 0, str_size); |
| 81 | |
| 82 | int res = set_ptr_to_sysfs(TC_WRITE_USER, buf, |
| 83 | "write from kernel-space to user-space"); |
| 84 | if (res) { |
| 85 | tst_resm(TFAIL, "failed to write from kernel"); |
| 86 | return; |
| 87 | } |
| 88 | |
| 89 | res = strncmp(buf, test_str, str_size) ? TFAIL : TPASS; |
| 90 | tst_resm(res, "copy_to_user(), put_user(): strings%sequal", |
| 91 | (res) ? " not " : " "); |
| 92 | } |
| 93 | |
| 94 | int main(int argc, char *argv[]) |
| 95 | { |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 96 | const char *msg; |
Alexey Kodanev | ddf49bf | 2013-12-04 13:27:31 +0400 | [diff] [blame] | 97 | msg = parse_opts(argc, argv, NULL, NULL); |
| 98 | if (msg != NULL) |
| 99 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
| 100 | |
| 101 | tst_require_root(NULL); |
| 102 | |
| 103 | if (tst_kvercmp(2, 6, 0) < 0) { |
| 104 | tst_brkm(TCONF, NULL, |
| 105 | "Test must be run with kernel 2.6 or newer"); |
| 106 | } |
| 107 | |
| 108 | tst_sig(FORK, DEF_HANDLER, cleanup); |
| 109 | |
| 110 | tst_module_load(NULL, module_name, NULL); |
| 111 | module_loaded = 1; |
| 112 | |
| 113 | tc_read_userspace(); |
| 114 | tc_write_userspace(); |
| 115 | |
| 116 | cleanup(); |
| 117 | tst_exit(); |
| 118 | } |