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