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"
30#include "old_module.h"
31#include "safe_macros.h"
32
33#include "ltp_uaccess.h"
34
35char *TCID = DEV_NAME;
36
37static const char dev_result[]	= "/sys/devices/" DEV_NAME "/result";
38static const char dev_tcase[]	= "/sys/devices/" DEV_NAME "/tcase";
39static const char module_name[]	= DEV_NAME ".ko";
40static int module_loaded;
41
42static void cleanup(void)
43{
44	if (module_loaded)
45		tst_module_unload(NULL, module_name);
46}
47
48static int set_ptr_to_sysfs(int id, const void *ptr, const char *descr)
49{
50	int res;
51	tst_resm(TINFO, "TC %d: %s, ptr '%p'", id, descr, ptr);
52	SAFE_FILE_PRINTF(cleanup, dev_tcase, "%d %lu", id, (unsigned long) ptr);
53	SAFE_FILE_SCANF(cleanup, dev_result, "%d", &res);
54	if (res)
55		return TFAIL;
56
57	return TPASS;
58}
59
60/*
61 * Read user-space memory using copy_from_user(), get_user().
62 */
63static void tc_read_userspace(void)
64{
65	int res = set_ptr_to_sysfs(TC_READ_USER, test_str,
66		"read user-space memory from kernel");
67
68	tst_resm(res, "copy_from_user(), get_user(): strings%sequal",
69		(res) ? " not " : " ");
70}
71
72/*
73 * Write from kernel-space to user-space
74 * using copy_to_user(), put_user().
75 */
76static void tc_write_userspace(void)
77{
78	char buf[str_size];
79	memset(buf, 0, str_size);
80
81	int res = set_ptr_to_sysfs(TC_WRITE_USER, buf,
82		"write from kernel-space to user-space");
83	if (res) {
84		tst_resm(TFAIL, "failed to write from kernel");
85		return;
86	}
87
88	res = strncmp(buf, test_str, str_size) ? TFAIL : TPASS;
89	tst_resm(res, "copy_to_user(), put_user(): strings%sequal",
90		(res) ? " not " : " ");
91}
92
93int main(int argc, char *argv[])
94{
95	tst_parse_opts(argc, argv, NULL, NULL);
96
97	tst_require_root();
98
99	tst_sig(FORK, DEF_HANDLER, cleanup);
100
101	tst_module_load(NULL, module_name, NULL);
102	module_loaded = 1;
103
104	tc_read_userspace();
105	tc_write_userspace();
106
107	cleanup();
108	tst_exit();
109}
110