1f08c3bdfSopenharmony_ci/******************************************************************************/ 2f08c3bdfSopenharmony_ci/* This program is free software; you can redistribute it and/or modify */ 3f08c3bdfSopenharmony_ci/* it under the terms of the GNU General Public License as published by */ 4f08c3bdfSopenharmony_ci/* the Free Software Foundation; either version 2 of the License, or */ 5f08c3bdfSopenharmony_ci/* (at your option) any later version. */ 6f08c3bdfSopenharmony_ci/* */ 7f08c3bdfSopenharmony_ci/* This program is distributed in the hope that it will be useful, */ 8f08c3bdfSopenharmony_ci/* but WITHOUT ANY WARRANTY; without even the implied warranty of */ 9f08c3bdfSopenharmony_ci/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See */ 10f08c3bdfSopenharmony_ci/* the GNU General Public License for more details. */ 11f08c3bdfSopenharmony_ci/* */ 12f08c3bdfSopenharmony_ci/* You should have received a copy of the GNU General Public License */ 13f08c3bdfSopenharmony_ci/* along with this program; if not, write to the Free Software */ 14f08c3bdfSopenharmony_ci/* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ 15f08c3bdfSopenharmony_ci/* */ 16f08c3bdfSopenharmony_ci/******************************************************************************/ 17f08c3bdfSopenharmony_ci/* 18f08c3bdfSopenharmony_ci * tomoyo_rewrite_test.c 19f08c3bdfSopenharmony_ci * 20f08c3bdfSopenharmony_ci * Testing program for security/tomoyo/ 21f08c3bdfSopenharmony_ci * 22f08c3bdfSopenharmony_ci * Copyright (C) 2005-2010 NTT DATA CORPORATION 23f08c3bdfSopenharmony_ci */ 24f08c3bdfSopenharmony_ci#include "include.h" 25f08c3bdfSopenharmony_ci 26f08c3bdfSopenharmony_cistatic int should_fail = 0; 27f08c3bdfSopenharmony_ci 28f08c3bdfSopenharmony_cistatic void show_prompt(const char *str) 29f08c3bdfSopenharmony_ci{ 30f08c3bdfSopenharmony_ci printf("Testing %35s: (%s) ", str, 31f08c3bdfSopenharmony_ci should_fail ? "must fail" : "must success"); 32f08c3bdfSopenharmony_ci errno = 0; 33f08c3bdfSopenharmony_ci} 34f08c3bdfSopenharmony_ci 35f08c3bdfSopenharmony_cistatic void show_result(int result) 36f08c3bdfSopenharmony_ci{ 37f08c3bdfSopenharmony_ci if (should_fail) { 38f08c3bdfSopenharmony_ci if (result == EOF) { 39f08c3bdfSopenharmony_ci if (errno == EPERM) 40f08c3bdfSopenharmony_ci printf("OK: Permission denied.\n"); 41f08c3bdfSopenharmony_ci else 42f08c3bdfSopenharmony_ci printf("BUG!\n"); 43f08c3bdfSopenharmony_ci } else { 44f08c3bdfSopenharmony_ci printf("BUG!\n"); 45f08c3bdfSopenharmony_ci } 46f08c3bdfSopenharmony_ci } else { 47f08c3bdfSopenharmony_ci if (result != EOF) 48f08c3bdfSopenharmony_ci printf("OK\n"); 49f08c3bdfSopenharmony_ci else 50f08c3bdfSopenharmony_ci printf("BUG!\n"); 51f08c3bdfSopenharmony_ci } 52f08c3bdfSopenharmony_ci} 53f08c3bdfSopenharmony_ci 54f08c3bdfSopenharmony_ci#define REWRITE_PATH "/tmp/rewrite_test" 55f08c3bdfSopenharmony_ci 56f08c3bdfSopenharmony_cistatic void stage_rewrite_test(void) 57f08c3bdfSopenharmony_ci{ 58f08c3bdfSopenharmony_ci int fd; 59f08c3bdfSopenharmony_ci 60f08c3bdfSopenharmony_ci /* Start up */ 61f08c3bdfSopenharmony_ci write_domain_policy("allow_read/write " REWRITE_PATH, 0); 62f08c3bdfSopenharmony_ci write_domain_policy("allow_truncate " REWRITE_PATH, 0); 63f08c3bdfSopenharmony_ci write_domain_policy("allow_create " REWRITE_PATH " 0600", 0); 64f08c3bdfSopenharmony_ci write_domain_policy("allow_unlink " REWRITE_PATH, 0); 65f08c3bdfSopenharmony_ci write_exception_policy("deny_rewrite " REWRITE_PATH, 0); 66f08c3bdfSopenharmony_ci set_profile(3, "file::open"); 67f08c3bdfSopenharmony_ci set_profile(3, "file::create"); 68f08c3bdfSopenharmony_ci set_profile(3, "file::truncate"); 69f08c3bdfSopenharmony_ci set_profile(3, "file::rewrite"); 70f08c3bdfSopenharmony_ci set_profile(3, "file::unlink"); 71f08c3bdfSopenharmony_ci close(open(REWRITE_PATH, O_WRONLY | O_APPEND | O_CREAT, 0600)); 72f08c3bdfSopenharmony_ci 73f08c3bdfSopenharmony_ci /* Enforce mode */ 74f08c3bdfSopenharmony_ci should_fail = 0; 75f08c3bdfSopenharmony_ci 76f08c3bdfSopenharmony_ci show_prompt("open(O_RDONLY)"); 77f08c3bdfSopenharmony_ci fd = open(REWRITE_PATH, O_RDONLY); 78f08c3bdfSopenharmony_ci show_result(fd); 79f08c3bdfSopenharmony_ci close(fd); 80f08c3bdfSopenharmony_ci 81f08c3bdfSopenharmony_ci show_prompt("open(O_WRONLY | O_APPEND)"); 82f08c3bdfSopenharmony_ci fd = open(REWRITE_PATH, O_WRONLY | O_APPEND); 83f08c3bdfSopenharmony_ci show_result(fd); 84f08c3bdfSopenharmony_ci close(fd); 85f08c3bdfSopenharmony_ci 86f08c3bdfSopenharmony_ci should_fail = 1; 87f08c3bdfSopenharmony_ci show_prompt("open(O_WRONLY)"); 88f08c3bdfSopenharmony_ci fd = open(REWRITE_PATH, O_WRONLY); 89f08c3bdfSopenharmony_ci show_result(fd); 90f08c3bdfSopenharmony_ci close(fd); 91f08c3bdfSopenharmony_ci 92f08c3bdfSopenharmony_ci show_prompt("open(O_WRONLY | O_TRUNC)"); 93f08c3bdfSopenharmony_ci fd = open(REWRITE_PATH, O_WRONLY | O_TRUNC); 94f08c3bdfSopenharmony_ci show_result(fd); 95f08c3bdfSopenharmony_ci close(fd); 96f08c3bdfSopenharmony_ci 97f08c3bdfSopenharmony_ci show_prompt("open(O_WRONLY | O_TRUNC | O_APPEND)"); 98f08c3bdfSopenharmony_ci fd = open(REWRITE_PATH, O_WRONLY | O_TRUNC | O_APPEND); 99f08c3bdfSopenharmony_ci show_result(fd); 100f08c3bdfSopenharmony_ci close(fd); 101f08c3bdfSopenharmony_ci 102f08c3bdfSopenharmony_ci show_prompt("truncate()"); 103f08c3bdfSopenharmony_ci show_result(truncate(REWRITE_PATH, 0)); 104f08c3bdfSopenharmony_ci 105f08c3bdfSopenharmony_ci fd = open(REWRITE_PATH, O_WRONLY | O_APPEND); 106f08c3bdfSopenharmony_ci show_prompt("ftruncate()"); 107f08c3bdfSopenharmony_ci show_result(ftruncate(fd, 0)); 108f08c3bdfSopenharmony_ci 109f08c3bdfSopenharmony_ci show_prompt("fcntl(F_SETFL, ~O_APPEND)"); 110f08c3bdfSopenharmony_ci show_result(fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) & ~O_APPEND)); 111f08c3bdfSopenharmony_ci close(fd); 112f08c3bdfSopenharmony_ci 113f08c3bdfSopenharmony_ci /* Permissive mode */ 114f08c3bdfSopenharmony_ci set_profile(2, "file::open"); 115f08c3bdfSopenharmony_ci set_profile(2, "file::create"); 116f08c3bdfSopenharmony_ci set_profile(2, "file::truncate"); 117f08c3bdfSopenharmony_ci set_profile(2, "file::rewrite"); 118f08c3bdfSopenharmony_ci set_profile(2, "file::unlink"); 119f08c3bdfSopenharmony_ci should_fail = 0; 120f08c3bdfSopenharmony_ci 121f08c3bdfSopenharmony_ci show_prompt("open(O_RDONLY)"); 122f08c3bdfSopenharmony_ci fd = open(REWRITE_PATH, O_RDONLY); 123f08c3bdfSopenharmony_ci show_result(fd); 124f08c3bdfSopenharmony_ci close(fd); 125f08c3bdfSopenharmony_ci 126f08c3bdfSopenharmony_ci show_prompt("open(O_WRONLY | O_APPEND)"); 127f08c3bdfSopenharmony_ci fd = open(REWRITE_PATH, O_WRONLY | O_APPEND); 128f08c3bdfSopenharmony_ci show_result(fd); 129f08c3bdfSopenharmony_ci close(fd); 130f08c3bdfSopenharmony_ci 131f08c3bdfSopenharmony_ci show_prompt("open(O_WRONLY)"); 132f08c3bdfSopenharmony_ci fd = open(REWRITE_PATH, O_WRONLY); 133f08c3bdfSopenharmony_ci show_result(fd); 134f08c3bdfSopenharmony_ci close(fd); 135f08c3bdfSopenharmony_ci 136f08c3bdfSopenharmony_ci show_prompt("open(O_WRONLY | O_TRUNC)"); 137f08c3bdfSopenharmony_ci fd = open(REWRITE_PATH, O_WRONLY | O_TRUNC); 138f08c3bdfSopenharmony_ci show_result(fd); 139f08c3bdfSopenharmony_ci close(fd); 140f08c3bdfSopenharmony_ci 141f08c3bdfSopenharmony_ci show_prompt("open(O_WRONLY | O_TRUNC | O_APPEND)"); 142f08c3bdfSopenharmony_ci fd = open(REWRITE_PATH, O_WRONLY | O_TRUNC | O_APPEND); 143f08c3bdfSopenharmony_ci show_result(fd); 144f08c3bdfSopenharmony_ci close(fd); 145f08c3bdfSopenharmony_ci 146f08c3bdfSopenharmony_ci show_prompt("truncate()"); 147f08c3bdfSopenharmony_ci show_result(truncate(REWRITE_PATH, 0)); 148f08c3bdfSopenharmony_ci 149f08c3bdfSopenharmony_ci fd = open(REWRITE_PATH, O_WRONLY | O_APPEND); 150f08c3bdfSopenharmony_ci show_prompt("ftruncate()"); 151f08c3bdfSopenharmony_ci show_result(ftruncate(fd, 0)); 152f08c3bdfSopenharmony_ci 153f08c3bdfSopenharmony_ci show_prompt("fcntl(F_SETFL, ~O_APPEND)"); 154f08c3bdfSopenharmony_ci show_result(fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) & ~O_APPEND)); 155f08c3bdfSopenharmony_ci close(fd); 156f08c3bdfSopenharmony_ci 157f08c3bdfSopenharmony_ci /* Clean up */ 158f08c3bdfSopenharmony_ci unlink(REWRITE_PATH); 159f08c3bdfSopenharmony_ci write_exception_policy("deny_rewrite " REWRITE_PATH, 0); 160f08c3bdfSopenharmony_ci printf("\n\n"); 161f08c3bdfSopenharmony_ci} 162f08c3bdfSopenharmony_ci 163f08c3bdfSopenharmony_ciint main(void) 164f08c3bdfSopenharmony_ci{ 165f08c3bdfSopenharmony_ci tomoyo_test_init(); 166f08c3bdfSopenharmony_ci stage_rewrite_test(); 167f08c3bdfSopenharmony_ci clear_status(); 168f08c3bdfSopenharmony_ci return 0; 169f08c3bdfSopenharmony_ci} 170