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