Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 1 | /* |
| 2 | * security/tomoyo/util.c |
| 3 | * |
| 4 | * Utility functions for TOMOYO. |
| 5 | * |
| 6 | * Copyright (C) 2005-2010 NTT DATA CORPORATION |
| 7 | */ |
| 8 | |
| 9 | #include <linux/slab.h> |
| 10 | #include "common.h" |
| 11 | |
| 12 | /* Lock for protecting policy. */ |
| 13 | DEFINE_MUTEX(tomoyo_policy_lock); |
| 14 | |
| 15 | /* Has /sbin/init started? */ |
| 16 | bool tomoyo_policy_loaded; |
| 17 | |
Tetsuo Handa | 2c47ab9 | 2011-06-26 23:21:19 +0900 | [diff] [blame] | 18 | /* |
| 19 | * Mapping table from "enum tomoyo_mac_index" to |
| 20 | * "enum tomoyo_mac_category_index". |
| 21 | */ |
| 22 | const u8 tomoyo_index2category[TOMOYO_MAX_MAC_INDEX] = { |
| 23 | /* CONFIG::file group */ |
| 24 | [TOMOYO_MAC_FILE_EXECUTE] = TOMOYO_MAC_CATEGORY_FILE, |
| 25 | [TOMOYO_MAC_FILE_OPEN] = TOMOYO_MAC_CATEGORY_FILE, |
| 26 | [TOMOYO_MAC_FILE_CREATE] = TOMOYO_MAC_CATEGORY_FILE, |
| 27 | [TOMOYO_MAC_FILE_UNLINK] = TOMOYO_MAC_CATEGORY_FILE, |
| 28 | [TOMOYO_MAC_FILE_GETATTR] = TOMOYO_MAC_CATEGORY_FILE, |
| 29 | [TOMOYO_MAC_FILE_MKDIR] = TOMOYO_MAC_CATEGORY_FILE, |
| 30 | [TOMOYO_MAC_FILE_RMDIR] = TOMOYO_MAC_CATEGORY_FILE, |
| 31 | [TOMOYO_MAC_FILE_MKFIFO] = TOMOYO_MAC_CATEGORY_FILE, |
| 32 | [TOMOYO_MAC_FILE_MKSOCK] = TOMOYO_MAC_CATEGORY_FILE, |
| 33 | [TOMOYO_MAC_FILE_TRUNCATE] = TOMOYO_MAC_CATEGORY_FILE, |
| 34 | [TOMOYO_MAC_FILE_SYMLINK] = TOMOYO_MAC_CATEGORY_FILE, |
| 35 | [TOMOYO_MAC_FILE_MKBLOCK] = TOMOYO_MAC_CATEGORY_FILE, |
| 36 | [TOMOYO_MAC_FILE_MKCHAR] = TOMOYO_MAC_CATEGORY_FILE, |
| 37 | [TOMOYO_MAC_FILE_LINK] = TOMOYO_MAC_CATEGORY_FILE, |
| 38 | [TOMOYO_MAC_FILE_RENAME] = TOMOYO_MAC_CATEGORY_FILE, |
| 39 | [TOMOYO_MAC_FILE_CHMOD] = TOMOYO_MAC_CATEGORY_FILE, |
| 40 | [TOMOYO_MAC_FILE_CHOWN] = TOMOYO_MAC_CATEGORY_FILE, |
| 41 | [TOMOYO_MAC_FILE_CHGRP] = TOMOYO_MAC_CATEGORY_FILE, |
| 42 | [TOMOYO_MAC_FILE_IOCTL] = TOMOYO_MAC_CATEGORY_FILE, |
| 43 | [TOMOYO_MAC_FILE_CHROOT] = TOMOYO_MAC_CATEGORY_FILE, |
| 44 | [TOMOYO_MAC_FILE_MOUNT] = TOMOYO_MAC_CATEGORY_FILE, |
| 45 | [TOMOYO_MAC_FILE_UMOUNT] = TOMOYO_MAC_CATEGORY_FILE, |
| 46 | [TOMOYO_MAC_FILE_PIVOT_ROOT] = TOMOYO_MAC_CATEGORY_FILE, |
| 47 | }; |
| 48 | |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 49 | /** |
Tetsuo Handa | b22b8b9 | 2011-06-26 23:21:50 +0900 | [diff] [blame^] | 50 | * tomoyo_convert_time - Convert time_t to YYYY/MM/DD hh/mm/ss. |
| 51 | * |
| 52 | * @time: Seconds since 1970/01/01 00:00:00. |
| 53 | * @stamp: Pointer to "struct tomoyo_time". |
| 54 | * |
| 55 | * Returns nothing. |
| 56 | * |
| 57 | * This function does not handle Y2038 problem. |
| 58 | */ |
| 59 | void tomoyo_convert_time(time_t time, struct tomoyo_time *stamp) |
| 60 | { |
| 61 | static const u16 tomoyo_eom[2][12] = { |
| 62 | { 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 }, |
| 63 | { 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 } |
| 64 | }; |
| 65 | u16 y; |
| 66 | u8 m; |
| 67 | bool r; |
| 68 | stamp->sec = time % 60; |
| 69 | time /= 60; |
| 70 | stamp->min = time % 60; |
| 71 | time /= 60; |
| 72 | stamp->hour = time % 24; |
| 73 | time /= 24; |
| 74 | for (y = 1970; ; y++) { |
| 75 | const unsigned short days = (y & 3) ? 365 : 366; |
| 76 | if (time < days) |
| 77 | break; |
| 78 | time -= days; |
| 79 | } |
| 80 | r = (y & 3) == 0; |
| 81 | for (m = 0; m < 11 && time >= tomoyo_eom[r][m]; m++) |
| 82 | ; |
| 83 | if (m) |
| 84 | time -= tomoyo_eom[r][m - 1]; |
| 85 | stamp->year = y; |
| 86 | stamp->month = ++m; |
| 87 | stamp->day = ++time; |
| 88 | } |
| 89 | |
| 90 | /** |
Tetsuo Handa | a238cf5 | 2011-06-26 23:17:10 +0900 | [diff] [blame] | 91 | * tomoyo_permstr - Find permission keywords. |
| 92 | * |
| 93 | * @string: String representation for permissions in foo/bar/buz format. |
| 94 | * @keyword: Keyword to find from @string/ |
| 95 | * |
| 96 | * Returns ture if @keyword was found in @string, false otherwise. |
| 97 | * |
| 98 | * This function assumes that strncmp(w1, w2, strlen(w1)) != 0 if w1 != w2. |
| 99 | */ |
| 100 | bool tomoyo_permstr(const char *string, const char *keyword) |
| 101 | { |
| 102 | const char *cp = strstr(string, keyword); |
| 103 | if (cp) |
| 104 | return cp == string || *(cp - 1) == '/'; |
| 105 | return false; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * tomoyo_read_token - Read a word from a line. |
| 110 | * |
| 111 | * @param: Pointer to "struct tomoyo_acl_param". |
| 112 | * |
| 113 | * Returns a word on success, "" otherwise. |
| 114 | * |
| 115 | * To allow the caller to skip NULL check, this function returns "" rather than |
| 116 | * NULL if there is no more words to read. |
| 117 | */ |
| 118 | char *tomoyo_read_token(struct tomoyo_acl_param *param) |
| 119 | { |
| 120 | char *pos = param->data; |
| 121 | char *del = strchr(pos, ' '); |
| 122 | if (del) |
| 123 | *del++ = '\0'; |
| 124 | else |
| 125 | del = pos + strlen(pos); |
| 126 | param->data = del; |
| 127 | return pos; |
| 128 | } |
| 129 | |
| 130 | /** |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 131 | * tomoyo_parse_ulong - Parse an "unsigned long" value. |
| 132 | * |
| 133 | * @result: Pointer to "unsigned long". |
| 134 | * @str: Pointer to string to parse. |
| 135 | * |
Tetsuo Handa | 0df7e8b | 2011-06-26 23:16:36 +0900 | [diff] [blame] | 136 | * Returns one of values in "enum tomoyo_value_type". |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 137 | * |
| 138 | * The @src is updated to point the first character after the value |
| 139 | * on success. |
| 140 | */ |
Tetsuo Handa | e2bf690 | 2010-06-25 11:16:00 +0900 | [diff] [blame] | 141 | static u8 tomoyo_parse_ulong(unsigned long *result, char **str) |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 142 | { |
| 143 | const char *cp = *str; |
| 144 | char *ep; |
| 145 | int base = 10; |
| 146 | if (*cp == '0') { |
| 147 | char c = *(cp + 1); |
| 148 | if (c == 'x' || c == 'X') { |
| 149 | base = 16; |
| 150 | cp += 2; |
| 151 | } else if (c >= '0' && c <= '7') { |
| 152 | base = 8; |
| 153 | cp++; |
| 154 | } |
| 155 | } |
| 156 | *result = simple_strtoul(cp, &ep, base); |
| 157 | if (cp == ep) |
Tetsuo Handa | 0df7e8b | 2011-06-26 23:16:36 +0900 | [diff] [blame] | 158 | return TOMOYO_VALUE_TYPE_INVALID; |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 159 | *str = ep; |
| 160 | switch (base) { |
| 161 | case 16: |
| 162 | return TOMOYO_VALUE_TYPE_HEXADECIMAL; |
| 163 | case 8: |
| 164 | return TOMOYO_VALUE_TYPE_OCTAL; |
| 165 | default: |
| 166 | return TOMOYO_VALUE_TYPE_DECIMAL; |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * tomoyo_print_ulong - Print an "unsigned long" value. |
| 172 | * |
| 173 | * @buffer: Pointer to buffer. |
| 174 | * @buffer_len: Size of @buffer. |
| 175 | * @value: An "unsigned long" value. |
| 176 | * @type: Type of @value. |
| 177 | * |
| 178 | * Returns nothing. |
| 179 | */ |
| 180 | void tomoyo_print_ulong(char *buffer, const int buffer_len, |
| 181 | const unsigned long value, const u8 type) |
| 182 | { |
| 183 | if (type == TOMOYO_VALUE_TYPE_DECIMAL) |
| 184 | snprintf(buffer, buffer_len, "%lu", value); |
| 185 | else if (type == TOMOYO_VALUE_TYPE_OCTAL) |
| 186 | snprintf(buffer, buffer_len, "0%lo", value); |
| 187 | else if (type == TOMOYO_VALUE_TYPE_HEXADECIMAL) |
| 188 | snprintf(buffer, buffer_len, "0x%lX", value); |
| 189 | else |
| 190 | snprintf(buffer, buffer_len, "type(%u)", type); |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * tomoyo_parse_name_union - Parse a tomoyo_name_union. |
| 195 | * |
Tetsuo Handa | a238cf5 | 2011-06-26 23:17:10 +0900 | [diff] [blame] | 196 | * @param: Pointer to "struct tomoyo_acl_param". |
| 197 | * @ptr: Pointer to "struct tomoyo_name_union". |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 198 | * |
| 199 | * Returns true on success, false otherwise. |
| 200 | */ |
Tetsuo Handa | a238cf5 | 2011-06-26 23:17:10 +0900 | [diff] [blame] | 201 | bool tomoyo_parse_name_union(struct tomoyo_acl_param *param, |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 202 | struct tomoyo_name_union *ptr) |
| 203 | { |
Tetsuo Handa | a238cf5 | 2011-06-26 23:17:10 +0900 | [diff] [blame] | 204 | char *filename; |
| 205 | if (param->data[0] == '@') { |
| 206 | param->data++; |
| 207 | ptr->group = tomoyo_get_group(param, TOMOYO_PATH_GROUP); |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 208 | return ptr->group != NULL; |
| 209 | } |
Tetsuo Handa | a238cf5 | 2011-06-26 23:17:10 +0900 | [diff] [blame] | 210 | filename = tomoyo_read_token(param); |
| 211 | if (!tomoyo_correct_word(filename)) |
| 212 | return false; |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 213 | ptr->filename = tomoyo_get_name(filename); |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 214 | return ptr->filename != NULL; |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * tomoyo_parse_number_union - Parse a tomoyo_number_union. |
| 219 | * |
Tetsuo Handa | a238cf5 | 2011-06-26 23:17:10 +0900 | [diff] [blame] | 220 | * @param: Pointer to "struct tomoyo_acl_param". |
| 221 | * @ptr: Pointer to "struct tomoyo_number_union". |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 222 | * |
| 223 | * Returns true on success, false otherwise. |
| 224 | */ |
Tetsuo Handa | a238cf5 | 2011-06-26 23:17:10 +0900 | [diff] [blame] | 225 | bool tomoyo_parse_number_union(struct tomoyo_acl_param *param, |
| 226 | struct tomoyo_number_union *ptr) |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 227 | { |
Tetsuo Handa | a238cf5 | 2011-06-26 23:17:10 +0900 | [diff] [blame] | 228 | char *data; |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 229 | u8 type; |
| 230 | unsigned long v; |
Tetsuo Handa | a238cf5 | 2011-06-26 23:17:10 +0900 | [diff] [blame] | 231 | memset(ptr, 0, sizeof(*ptr)); |
| 232 | if (param->data[0] == '@') { |
| 233 | param->data++; |
| 234 | ptr->group = tomoyo_get_group(param, TOMOYO_NUMBER_GROUP); |
| 235 | return ptr->group != NULL; |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 236 | } |
Tetsuo Handa | a238cf5 | 2011-06-26 23:17:10 +0900 | [diff] [blame] | 237 | data = tomoyo_read_token(param); |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 238 | type = tomoyo_parse_ulong(&v, &data); |
Tetsuo Handa | a238cf5 | 2011-06-26 23:17:10 +0900 | [diff] [blame] | 239 | if (type == TOMOYO_VALUE_TYPE_INVALID) |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 240 | return false; |
Tetsuo Handa | a238cf5 | 2011-06-26 23:17:10 +0900 | [diff] [blame] | 241 | ptr->values[0] = v; |
| 242 | ptr->value_type[0] = type; |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 243 | if (!*data) { |
Tetsuo Handa | a238cf5 | 2011-06-26 23:17:10 +0900 | [diff] [blame] | 244 | ptr->values[1] = v; |
| 245 | ptr->value_type[1] = type; |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 246 | return true; |
| 247 | } |
| 248 | if (*data++ != '-') |
| 249 | return false; |
| 250 | type = tomoyo_parse_ulong(&v, &data); |
Tetsuo Handa | a238cf5 | 2011-06-26 23:17:10 +0900 | [diff] [blame] | 251 | if (type == TOMOYO_VALUE_TYPE_INVALID || *data || ptr->values[0] > v) |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 252 | return false; |
Tetsuo Handa | a238cf5 | 2011-06-26 23:17:10 +0900 | [diff] [blame] | 253 | ptr->values[1] = v; |
| 254 | ptr->value_type[1] = type; |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 255 | return true; |
| 256 | } |
| 257 | |
| 258 | /** |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 259 | * tomoyo_byte_range - Check whether the string is a \ooo style octal value. |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 260 | * |
| 261 | * @str: Pointer to the string. |
| 262 | * |
| 263 | * Returns true if @str is a \ooo style octal value, false otherwise. |
| 264 | * |
| 265 | * TOMOYO uses \ooo style representation for 0x01 - 0x20 and 0x7F - 0xFF. |
| 266 | * This function verifies that \ooo is in valid range. |
| 267 | */ |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 268 | static inline bool tomoyo_byte_range(const char *str) |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 269 | { |
| 270 | return *str >= '0' && *str++ <= '3' && |
| 271 | *str >= '0' && *str++ <= '7' && |
| 272 | *str >= '0' && *str <= '7'; |
| 273 | } |
| 274 | |
| 275 | /** |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 276 | * tomoyo_alphabet_char - Check whether the character is an alphabet. |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 277 | * |
| 278 | * @c: The character to check. |
| 279 | * |
| 280 | * Returns true if @c is an alphabet character, false otherwise. |
| 281 | */ |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 282 | static inline bool tomoyo_alphabet_char(const char c) |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 283 | { |
| 284 | return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'); |
| 285 | } |
| 286 | |
| 287 | /** |
| 288 | * tomoyo_make_byte - Make byte value from three octal characters. |
| 289 | * |
| 290 | * @c1: The first character. |
| 291 | * @c2: The second character. |
| 292 | * @c3: The third character. |
| 293 | * |
| 294 | * Returns byte value. |
| 295 | */ |
| 296 | static inline u8 tomoyo_make_byte(const u8 c1, const u8 c2, const u8 c3) |
| 297 | { |
| 298 | return ((c1 - '0') << 6) + ((c2 - '0') << 3) + (c3 - '0'); |
| 299 | } |
| 300 | |
| 301 | /** |
Tetsuo Handa | 0df7e8b | 2011-06-26 23:16:36 +0900 | [diff] [blame] | 302 | * tomoyo_valid - Check whether the character is a valid char. |
| 303 | * |
| 304 | * @c: The character to check. |
| 305 | * |
| 306 | * Returns true if @c is a valid character, false otherwise. |
| 307 | */ |
| 308 | static inline bool tomoyo_valid(const unsigned char c) |
| 309 | { |
| 310 | return c > ' ' && c < 127; |
| 311 | } |
| 312 | |
| 313 | /** |
| 314 | * tomoyo_invalid - Check whether the character is an invalid char. |
| 315 | * |
| 316 | * @c: The character to check. |
| 317 | * |
| 318 | * Returns true if @c is an invalid character, false otherwise. |
| 319 | */ |
| 320 | static inline bool tomoyo_invalid(const unsigned char c) |
| 321 | { |
| 322 | return c && (c <= ' ' || c >= 127); |
| 323 | } |
| 324 | |
| 325 | /** |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 326 | * tomoyo_str_starts - Check whether the given string starts with the given keyword. |
| 327 | * |
| 328 | * @src: Pointer to pointer to the string. |
| 329 | * @find: Pointer to the keyword. |
| 330 | * |
| 331 | * Returns true if @src starts with @find, false otherwise. |
| 332 | * |
| 333 | * The @src is updated to point the first character after the @find |
| 334 | * if @src starts with @find. |
| 335 | */ |
| 336 | bool tomoyo_str_starts(char **src, const char *find) |
| 337 | { |
| 338 | const int len = strlen(find); |
| 339 | char *tmp = *src; |
| 340 | |
| 341 | if (strncmp(tmp, find, len)) |
| 342 | return false; |
| 343 | tmp += len; |
| 344 | *src = tmp; |
| 345 | return true; |
| 346 | } |
| 347 | |
| 348 | /** |
| 349 | * tomoyo_normalize_line - Format string. |
| 350 | * |
| 351 | * @buffer: The line to normalize. |
| 352 | * |
| 353 | * Leading and trailing whitespaces are removed. |
| 354 | * Multiple whitespaces are packed into single space. |
| 355 | * |
| 356 | * Returns nothing. |
| 357 | */ |
| 358 | void tomoyo_normalize_line(unsigned char *buffer) |
| 359 | { |
| 360 | unsigned char *sp = buffer; |
| 361 | unsigned char *dp = buffer; |
| 362 | bool first = true; |
| 363 | |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 364 | while (tomoyo_invalid(*sp)) |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 365 | sp++; |
| 366 | while (*sp) { |
| 367 | if (!first) |
| 368 | *dp++ = ' '; |
| 369 | first = false; |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 370 | while (tomoyo_valid(*sp)) |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 371 | *dp++ = *sp++; |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 372 | while (tomoyo_invalid(*sp)) |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 373 | sp++; |
| 374 | } |
| 375 | *dp = '\0'; |
| 376 | } |
| 377 | |
| 378 | /** |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 379 | * tomoyo_correct_word2 - Validate a string. |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 380 | * |
Tetsuo Handa | 3f62963 | 2010-06-03 20:37:26 +0900 | [diff] [blame] | 381 | * @string: The string to check. May be non-'\0'-terminated. |
| 382 | * @len: Length of @string. |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 383 | * |
Tetsuo Handa | 3f62963 | 2010-06-03 20:37:26 +0900 | [diff] [blame] | 384 | * Check whether the given string follows the naming rules. |
| 385 | * Returns true if @string follows the naming rules, false otherwise. |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 386 | */ |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 387 | static bool tomoyo_correct_word2(const char *string, size_t len) |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 388 | { |
Tetsuo Handa | 3f62963 | 2010-06-03 20:37:26 +0900 | [diff] [blame] | 389 | const char *const start = string; |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 390 | bool in_repetition = false; |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 391 | unsigned char c; |
| 392 | unsigned char d; |
| 393 | unsigned char e; |
Tetsuo Handa | 3f62963 | 2010-06-03 20:37:26 +0900 | [diff] [blame] | 394 | if (!len) |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 395 | goto out; |
Tetsuo Handa | 3f62963 | 2010-06-03 20:37:26 +0900 | [diff] [blame] | 396 | while (len--) { |
| 397 | c = *string++; |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 398 | if (c == '\\') { |
Tetsuo Handa | 3f62963 | 2010-06-03 20:37:26 +0900 | [diff] [blame] | 399 | if (!len--) |
| 400 | goto out; |
| 401 | c = *string++; |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 402 | switch (c) { |
| 403 | case '\\': /* "\\" */ |
| 404 | continue; |
| 405 | case '$': /* "\$" */ |
| 406 | case '+': /* "\+" */ |
| 407 | case '?': /* "\?" */ |
| 408 | case '*': /* "\*" */ |
| 409 | case '@': /* "\@" */ |
| 410 | case 'x': /* "\x" */ |
| 411 | case 'X': /* "\X" */ |
| 412 | case 'a': /* "\a" */ |
| 413 | case 'A': /* "\A" */ |
| 414 | case '-': /* "\-" */ |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 415 | continue; |
| 416 | case '{': /* "/\{" */ |
Tetsuo Handa | 3f62963 | 2010-06-03 20:37:26 +0900 | [diff] [blame] | 417 | if (string - 3 < start || *(string - 3) != '/') |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 418 | break; |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 419 | in_repetition = true; |
| 420 | continue; |
| 421 | case '}': /* "\}/" */ |
Tetsuo Handa | 3f62963 | 2010-06-03 20:37:26 +0900 | [diff] [blame] | 422 | if (*string != '/') |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 423 | break; |
| 424 | if (!in_repetition) |
| 425 | break; |
| 426 | in_repetition = false; |
| 427 | continue; |
| 428 | case '0': /* "\ooo" */ |
| 429 | case '1': |
| 430 | case '2': |
| 431 | case '3': |
Tetsuo Handa | 3f62963 | 2010-06-03 20:37:26 +0900 | [diff] [blame] | 432 | if (!len-- || !len--) |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 433 | break; |
Tetsuo Handa | 3f62963 | 2010-06-03 20:37:26 +0900 | [diff] [blame] | 434 | d = *string++; |
| 435 | e = *string++; |
| 436 | if (d < '0' || d > '7' || e < '0' || e > '7') |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 437 | break; |
| 438 | c = tomoyo_make_byte(c, d, e); |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 439 | if (tomoyo_invalid(c)) |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 440 | continue; /* pattern is not \000 */ |
| 441 | } |
| 442 | goto out; |
| 443 | } else if (in_repetition && c == '/') { |
| 444 | goto out; |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 445 | } else if (tomoyo_invalid(c)) { |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 446 | goto out; |
| 447 | } |
| 448 | } |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 449 | if (in_repetition) |
| 450 | goto out; |
| 451 | return true; |
| 452 | out: |
| 453 | return false; |
| 454 | } |
| 455 | |
| 456 | /** |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 457 | * tomoyo_correct_word - Validate a string. |
Tetsuo Handa | 3f62963 | 2010-06-03 20:37:26 +0900 | [diff] [blame] | 458 | * |
| 459 | * @string: The string to check. |
| 460 | * |
| 461 | * Check whether the given string follows the naming rules. |
| 462 | * Returns true if @string follows the naming rules, false otherwise. |
| 463 | */ |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 464 | bool tomoyo_correct_word(const char *string) |
Tetsuo Handa | 3f62963 | 2010-06-03 20:37:26 +0900 | [diff] [blame] | 465 | { |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 466 | return tomoyo_correct_word2(string, strlen(string)); |
Tetsuo Handa | 3f62963 | 2010-06-03 20:37:26 +0900 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | /** |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 470 | * tomoyo_correct_path - Validate a pathname. |
Tetsuo Handa | 3f62963 | 2010-06-03 20:37:26 +0900 | [diff] [blame] | 471 | * |
| 472 | * @filename: The pathname to check. |
| 473 | * |
| 474 | * Check whether the given pathname follows the naming rules. |
| 475 | * Returns true if @filename follows the naming rules, false otherwise. |
| 476 | */ |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 477 | bool tomoyo_correct_path(const char *filename) |
Tetsuo Handa | 3f62963 | 2010-06-03 20:37:26 +0900 | [diff] [blame] | 478 | { |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 479 | return *filename == '/' && tomoyo_correct_word(filename); |
Tetsuo Handa | 3f62963 | 2010-06-03 20:37:26 +0900 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | /** |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 483 | * tomoyo_correct_domain - Check whether the given domainname follows the naming rules. |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 484 | * |
Tetsuo Handa | 3f62963 | 2010-06-03 20:37:26 +0900 | [diff] [blame] | 485 | * @domainname: The domainname to check. |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 486 | * |
| 487 | * Returns true if @domainname follows the naming rules, false otherwise. |
| 488 | */ |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 489 | bool tomoyo_correct_domain(const unsigned char *domainname) |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 490 | { |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 491 | if (!domainname || !tomoyo_domain_def(domainname)) |
| 492 | return false; |
| 493 | domainname = strchr(domainname, ' '); |
| 494 | if (!domainname++) |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 495 | return true; |
Tetsuo Handa | 3f62963 | 2010-06-03 20:37:26 +0900 | [diff] [blame] | 496 | while (1) { |
| 497 | const unsigned char *cp = strchr(domainname, ' '); |
| 498 | if (!cp) |
| 499 | break; |
| 500 | if (*domainname != '/' || |
Tetsuo Handa | e77dc34 | 2011-05-12 06:40:51 +0900 | [diff] [blame] | 501 | !tomoyo_correct_word2(domainname, cp - domainname)) |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 502 | return false; |
Tetsuo Handa | 3f62963 | 2010-06-03 20:37:26 +0900 | [diff] [blame] | 503 | domainname = cp + 1; |
| 504 | } |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 505 | return tomoyo_correct_path(domainname); |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | /** |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 509 | * tomoyo_domain_def - Check whether the given token can be a domainname. |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 510 | * |
| 511 | * @buffer: The token to check. |
| 512 | * |
| 513 | * Returns true if @buffer possibly be a domainname, false otherwise. |
| 514 | */ |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 515 | bool tomoyo_domain_def(const unsigned char *buffer) |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 516 | { |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 517 | const unsigned char *cp; |
| 518 | int len; |
| 519 | if (*buffer != '<') |
| 520 | return false; |
| 521 | cp = strchr(buffer, ' '); |
| 522 | if (!cp) |
| 523 | len = strlen(buffer); |
| 524 | else |
| 525 | len = cp - buffer; |
| 526 | if (buffer[len - 1] != '>' || |
| 527 | !tomoyo_correct_word2(buffer + 1, len - 2)) |
| 528 | return false; |
| 529 | return true; |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | /** |
| 533 | * tomoyo_find_domain - Find a domain by the given name. |
| 534 | * |
| 535 | * @domainname: The domainname to find. |
| 536 | * |
| 537 | * Returns pointer to "struct tomoyo_domain_info" if found, NULL otherwise. |
| 538 | * |
| 539 | * Caller holds tomoyo_read_lock(). |
| 540 | */ |
| 541 | struct tomoyo_domain_info *tomoyo_find_domain(const char *domainname) |
| 542 | { |
| 543 | struct tomoyo_domain_info *domain; |
| 544 | struct tomoyo_path_info name; |
| 545 | |
| 546 | name.name = domainname; |
| 547 | tomoyo_fill_path_info(&name); |
| 548 | list_for_each_entry_rcu(domain, &tomoyo_domain_list, list) { |
| 549 | if (!domain->is_deleted && |
| 550 | !tomoyo_pathcmp(&name, domain->domainname)) |
| 551 | return domain; |
| 552 | } |
| 553 | return NULL; |
| 554 | } |
| 555 | |
| 556 | /** |
| 557 | * tomoyo_const_part_length - Evaluate the initial length without a pattern in a token. |
| 558 | * |
| 559 | * @filename: The string to evaluate. |
| 560 | * |
| 561 | * Returns the initial length without a pattern in @filename. |
| 562 | */ |
| 563 | static int tomoyo_const_part_length(const char *filename) |
| 564 | { |
| 565 | char c; |
| 566 | int len = 0; |
| 567 | |
| 568 | if (!filename) |
| 569 | return 0; |
| 570 | while ((c = *filename++) != '\0') { |
| 571 | if (c != '\\') { |
| 572 | len++; |
| 573 | continue; |
| 574 | } |
| 575 | c = *filename++; |
| 576 | switch (c) { |
| 577 | case '\\': /* "\\" */ |
| 578 | len += 2; |
| 579 | continue; |
| 580 | case '0': /* "\ooo" */ |
| 581 | case '1': |
| 582 | case '2': |
| 583 | case '3': |
| 584 | c = *filename++; |
| 585 | if (c < '0' || c > '7') |
| 586 | break; |
| 587 | c = *filename++; |
| 588 | if (c < '0' || c > '7') |
| 589 | break; |
| 590 | len += 4; |
| 591 | continue; |
| 592 | } |
| 593 | break; |
| 594 | } |
| 595 | return len; |
| 596 | } |
| 597 | |
| 598 | /** |
| 599 | * tomoyo_fill_path_info - Fill in "struct tomoyo_path_info" members. |
| 600 | * |
| 601 | * @ptr: Pointer to "struct tomoyo_path_info" to fill in. |
| 602 | * |
| 603 | * The caller sets "struct tomoyo_path_info"->name. |
| 604 | */ |
| 605 | void tomoyo_fill_path_info(struct tomoyo_path_info *ptr) |
| 606 | { |
| 607 | const char *name = ptr->name; |
| 608 | const int len = strlen(name); |
| 609 | |
| 610 | ptr->const_len = tomoyo_const_part_length(name); |
| 611 | ptr->is_dir = len && (name[len - 1] == '/'); |
| 612 | ptr->is_patterned = (ptr->const_len < len); |
| 613 | ptr->hash = full_name_hash(name, len); |
| 614 | } |
| 615 | |
| 616 | /** |
| 617 | * tomoyo_file_matches_pattern2 - Pattern matching without '/' character and "\-" pattern. |
| 618 | * |
| 619 | * @filename: The start of string to check. |
| 620 | * @filename_end: The end of string to check. |
| 621 | * @pattern: The start of pattern to compare. |
| 622 | * @pattern_end: The end of pattern to compare. |
| 623 | * |
| 624 | * Returns true if @filename matches @pattern, false otherwise. |
| 625 | */ |
| 626 | static bool tomoyo_file_matches_pattern2(const char *filename, |
| 627 | const char *filename_end, |
| 628 | const char *pattern, |
| 629 | const char *pattern_end) |
| 630 | { |
| 631 | while (filename < filename_end && pattern < pattern_end) { |
| 632 | char c; |
| 633 | if (*pattern != '\\') { |
| 634 | if (*filename++ != *pattern++) |
| 635 | return false; |
| 636 | continue; |
| 637 | } |
| 638 | c = *filename; |
| 639 | pattern++; |
| 640 | switch (*pattern) { |
| 641 | int i; |
| 642 | int j; |
| 643 | case '?': |
| 644 | if (c == '/') { |
| 645 | return false; |
| 646 | } else if (c == '\\') { |
| 647 | if (filename[1] == '\\') |
| 648 | filename++; |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 649 | else if (tomoyo_byte_range(filename + 1)) |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 650 | filename += 3; |
| 651 | else |
| 652 | return false; |
| 653 | } |
| 654 | break; |
| 655 | case '\\': |
| 656 | if (c != '\\') |
| 657 | return false; |
| 658 | if (*++filename != '\\') |
| 659 | return false; |
| 660 | break; |
| 661 | case '+': |
| 662 | if (!isdigit(c)) |
| 663 | return false; |
| 664 | break; |
| 665 | case 'x': |
| 666 | if (!isxdigit(c)) |
| 667 | return false; |
| 668 | break; |
| 669 | case 'a': |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 670 | if (!tomoyo_alphabet_char(c)) |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 671 | return false; |
| 672 | break; |
| 673 | case '0': |
| 674 | case '1': |
| 675 | case '2': |
| 676 | case '3': |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 677 | if (c == '\\' && tomoyo_byte_range(filename + 1) |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 678 | && strncmp(filename + 1, pattern, 3) == 0) { |
| 679 | filename += 3; |
| 680 | pattern += 2; |
| 681 | break; |
| 682 | } |
| 683 | return false; /* Not matched. */ |
| 684 | case '*': |
| 685 | case '@': |
| 686 | for (i = 0; i <= filename_end - filename; i++) { |
| 687 | if (tomoyo_file_matches_pattern2( |
| 688 | filename + i, filename_end, |
| 689 | pattern + 1, pattern_end)) |
| 690 | return true; |
| 691 | c = filename[i]; |
| 692 | if (c == '.' && *pattern == '@') |
| 693 | break; |
| 694 | if (c != '\\') |
| 695 | continue; |
| 696 | if (filename[i + 1] == '\\') |
| 697 | i++; |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 698 | else if (tomoyo_byte_range(filename + i + 1)) |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 699 | i += 3; |
| 700 | else |
| 701 | break; /* Bad pattern. */ |
| 702 | } |
| 703 | return false; /* Not matched. */ |
| 704 | default: |
| 705 | j = 0; |
| 706 | c = *pattern; |
| 707 | if (c == '$') { |
| 708 | while (isdigit(filename[j])) |
| 709 | j++; |
| 710 | } else if (c == 'X') { |
| 711 | while (isxdigit(filename[j])) |
| 712 | j++; |
| 713 | } else if (c == 'A') { |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 714 | while (tomoyo_alphabet_char(filename[j])) |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 715 | j++; |
| 716 | } |
| 717 | for (i = 1; i <= j; i++) { |
| 718 | if (tomoyo_file_matches_pattern2( |
| 719 | filename + i, filename_end, |
| 720 | pattern + 1, pattern_end)) |
| 721 | return true; |
| 722 | } |
| 723 | return false; /* Not matched or bad pattern. */ |
| 724 | } |
| 725 | filename++; |
| 726 | pattern++; |
| 727 | } |
| 728 | while (*pattern == '\\' && |
| 729 | (*(pattern + 1) == '*' || *(pattern + 1) == '@')) |
| 730 | pattern += 2; |
| 731 | return filename == filename_end && pattern == pattern_end; |
| 732 | } |
| 733 | |
| 734 | /** |
| 735 | * tomoyo_file_matches_pattern - Pattern matching without '/' character. |
| 736 | * |
| 737 | * @filename: The start of string to check. |
| 738 | * @filename_end: The end of string to check. |
| 739 | * @pattern: The start of pattern to compare. |
| 740 | * @pattern_end: The end of pattern to compare. |
| 741 | * |
| 742 | * Returns true if @filename matches @pattern, false otherwise. |
| 743 | */ |
| 744 | static bool tomoyo_file_matches_pattern(const char *filename, |
| 745 | const char *filename_end, |
| 746 | const char *pattern, |
| 747 | const char *pattern_end) |
| 748 | { |
| 749 | const char *pattern_start = pattern; |
| 750 | bool first = true; |
| 751 | bool result; |
| 752 | |
| 753 | while (pattern < pattern_end - 1) { |
| 754 | /* Split at "\-" pattern. */ |
| 755 | if (*pattern++ != '\\' || *pattern++ != '-') |
| 756 | continue; |
| 757 | result = tomoyo_file_matches_pattern2(filename, |
| 758 | filename_end, |
| 759 | pattern_start, |
| 760 | pattern - 2); |
| 761 | if (first) |
| 762 | result = !result; |
| 763 | if (result) |
| 764 | return false; |
| 765 | first = false; |
| 766 | pattern_start = pattern; |
| 767 | } |
| 768 | result = tomoyo_file_matches_pattern2(filename, filename_end, |
| 769 | pattern_start, pattern_end); |
| 770 | return first ? result : !result; |
| 771 | } |
| 772 | |
| 773 | /** |
| 774 | * tomoyo_path_matches_pattern2 - Do pathname pattern matching. |
| 775 | * |
| 776 | * @f: The start of string to check. |
| 777 | * @p: The start of pattern to compare. |
| 778 | * |
| 779 | * Returns true if @f matches @p, false otherwise. |
| 780 | */ |
| 781 | static bool tomoyo_path_matches_pattern2(const char *f, const char *p) |
| 782 | { |
| 783 | const char *f_delimiter; |
| 784 | const char *p_delimiter; |
| 785 | |
| 786 | while (*f && *p) { |
| 787 | f_delimiter = strchr(f, '/'); |
| 788 | if (!f_delimiter) |
| 789 | f_delimiter = f + strlen(f); |
| 790 | p_delimiter = strchr(p, '/'); |
| 791 | if (!p_delimiter) |
| 792 | p_delimiter = p + strlen(p); |
| 793 | if (*p == '\\' && *(p + 1) == '{') |
| 794 | goto recursive; |
| 795 | if (!tomoyo_file_matches_pattern(f, f_delimiter, p, |
| 796 | p_delimiter)) |
| 797 | return false; |
| 798 | f = f_delimiter; |
| 799 | if (*f) |
| 800 | f++; |
| 801 | p = p_delimiter; |
| 802 | if (*p) |
| 803 | p++; |
| 804 | } |
| 805 | /* Ignore trailing "\*" and "\@" in @pattern. */ |
| 806 | while (*p == '\\' && |
| 807 | (*(p + 1) == '*' || *(p + 1) == '@')) |
| 808 | p += 2; |
| 809 | return !*f && !*p; |
| 810 | recursive: |
| 811 | /* |
| 812 | * The "\{" pattern is permitted only after '/' character. |
| 813 | * This guarantees that below "*(p - 1)" is safe. |
| 814 | * Also, the "\}" pattern is permitted only before '/' character |
| 815 | * so that "\{" + "\}" pair will not break the "\-" operator. |
| 816 | */ |
| 817 | if (*(p - 1) != '/' || p_delimiter <= p + 3 || *p_delimiter != '/' || |
| 818 | *(p_delimiter - 1) != '}' || *(p_delimiter - 2) != '\\') |
| 819 | return false; /* Bad pattern. */ |
| 820 | do { |
| 821 | /* Compare current component with pattern. */ |
| 822 | if (!tomoyo_file_matches_pattern(f, f_delimiter, p + 2, |
| 823 | p_delimiter - 2)) |
| 824 | break; |
| 825 | /* Proceed to next component. */ |
| 826 | f = f_delimiter; |
| 827 | if (!*f) |
| 828 | break; |
| 829 | f++; |
| 830 | /* Continue comparison. */ |
| 831 | if (tomoyo_path_matches_pattern2(f, p_delimiter + 1)) |
| 832 | return true; |
| 833 | f_delimiter = strchr(f, '/'); |
| 834 | } while (f_delimiter); |
| 835 | return false; /* Not matched. */ |
| 836 | } |
| 837 | |
| 838 | /** |
| 839 | * tomoyo_path_matches_pattern - Check whether the given filename matches the given pattern. |
| 840 | * |
| 841 | * @filename: The filename to check. |
| 842 | * @pattern: The pattern to compare. |
| 843 | * |
| 844 | * Returns true if matches, false otherwise. |
| 845 | * |
| 846 | * The following patterns are available. |
| 847 | * \\ \ itself. |
| 848 | * \ooo Octal representation of a byte. |
| 849 | * \* Zero or more repetitions of characters other than '/'. |
| 850 | * \@ Zero or more repetitions of characters other than '/' or '.'. |
| 851 | * \? 1 byte character other than '/'. |
| 852 | * \$ One or more repetitions of decimal digits. |
| 853 | * \+ 1 decimal digit. |
| 854 | * \X One or more repetitions of hexadecimal digits. |
| 855 | * \x 1 hexadecimal digit. |
| 856 | * \A One or more repetitions of alphabet characters. |
| 857 | * \a 1 alphabet character. |
| 858 | * |
| 859 | * \- Subtraction operator. |
| 860 | * |
| 861 | * /\{dir\}/ '/' + 'One or more repetitions of dir/' (e.g. /dir/ /dir/dir/ |
| 862 | * /dir/dir/dir/ ). |
| 863 | */ |
| 864 | bool tomoyo_path_matches_pattern(const struct tomoyo_path_info *filename, |
| 865 | const struct tomoyo_path_info *pattern) |
| 866 | { |
| 867 | const char *f = filename->name; |
| 868 | const char *p = pattern->name; |
| 869 | const int len = pattern->const_len; |
| 870 | |
| 871 | /* If @pattern doesn't contain pattern, I can use strcmp(). */ |
| 872 | if (!pattern->is_patterned) |
| 873 | return !tomoyo_pathcmp(filename, pattern); |
| 874 | /* Don't compare directory and non-directory. */ |
| 875 | if (filename->is_dir != pattern->is_dir) |
| 876 | return false; |
| 877 | /* Compare the initial length without patterns. */ |
| 878 | if (strncmp(f, p, len)) |
| 879 | return false; |
| 880 | f += len; |
| 881 | p += len; |
| 882 | return tomoyo_path_matches_pattern2(f, p); |
| 883 | } |
| 884 | |
| 885 | /** |
| 886 | * tomoyo_get_exe - Get tomoyo_realpath() of current process. |
| 887 | * |
| 888 | * Returns the tomoyo_realpath() of current process on success, NULL otherwise. |
| 889 | * |
| 890 | * This function uses kzalloc(), so the caller must call kfree() |
| 891 | * if this function didn't return NULL. |
| 892 | */ |
| 893 | const char *tomoyo_get_exe(void) |
| 894 | { |
| 895 | struct mm_struct *mm = current->mm; |
| 896 | struct vm_area_struct *vma; |
| 897 | const char *cp = NULL; |
| 898 | |
| 899 | if (!mm) |
| 900 | return NULL; |
| 901 | down_read(&mm->mmap_sem); |
| 902 | for (vma = mm->mmap; vma; vma = vma->vm_next) { |
| 903 | if ((vma->vm_flags & VM_EXECUTABLE) && vma->vm_file) { |
| 904 | cp = tomoyo_realpath_from_path(&vma->vm_file->f_path); |
| 905 | break; |
| 906 | } |
| 907 | } |
| 908 | up_read(&mm->mmap_sem); |
| 909 | return cp; |
| 910 | } |
| 911 | |
| 912 | /** |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 913 | * tomoyo_get_mode - Get MAC mode. |
| 914 | * |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 915 | * @ns: Pointer to "struct tomoyo_policy_namespace". |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 916 | * @profile: Profile number. |
| 917 | * @index: Index number of functionality. |
| 918 | * |
| 919 | * Returns mode. |
| 920 | */ |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 921 | int tomoyo_get_mode(const struct tomoyo_policy_namespace *ns, const u8 profile, |
| 922 | const u8 index) |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 923 | { |
| 924 | u8 mode; |
| 925 | const u8 category = TOMOYO_MAC_CATEGORY_FILE; |
| 926 | if (!tomoyo_policy_loaded) |
| 927 | return TOMOYO_CONFIG_DISABLED; |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 928 | mode = tomoyo_profile(ns, profile)->config[index]; |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 929 | if (mode == TOMOYO_CONFIG_USE_DEFAULT) |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 930 | mode = tomoyo_profile(ns, profile)->config[category]; |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 931 | if (mode == TOMOYO_CONFIG_USE_DEFAULT) |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 932 | mode = tomoyo_profile(ns, profile)->default_config; |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 933 | return mode & 3; |
| 934 | } |
| 935 | |
| 936 | /** |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 937 | * tomoyo_init_request_info - Initialize "struct tomoyo_request_info" members. |
| 938 | * |
| 939 | * @r: Pointer to "struct tomoyo_request_info" to initialize. |
| 940 | * @domain: Pointer to "struct tomoyo_domain_info". NULL for tomoyo_domain(). |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 941 | * @index: Index number of functionality. |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 942 | * |
| 943 | * Returns mode. |
| 944 | */ |
| 945 | int tomoyo_init_request_info(struct tomoyo_request_info *r, |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 946 | struct tomoyo_domain_info *domain, const u8 index) |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 947 | { |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 948 | u8 profile; |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 949 | memset(r, 0, sizeof(*r)); |
| 950 | if (!domain) |
| 951 | domain = tomoyo_domain(); |
| 952 | r->domain = domain; |
Tetsuo Handa | 57c2590 | 2010-06-03 20:38:44 +0900 | [diff] [blame] | 953 | profile = domain->profile; |
| 954 | r->profile = profile; |
| 955 | r->type = index; |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 956 | r->mode = tomoyo_get_mode(domain->ns, profile, index); |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 957 | return r->mode; |
| 958 | } |
| 959 | |
| 960 | /** |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 961 | * tomoyo_domain_quota_is_ok - Check for domain's quota. |
| 962 | * |
| 963 | * @r: Pointer to "struct tomoyo_request_info". |
| 964 | * |
| 965 | * Returns true if the domain is not exceeded quota, false otherwise. |
| 966 | * |
| 967 | * Caller holds tomoyo_read_lock(). |
| 968 | */ |
| 969 | bool tomoyo_domain_quota_is_ok(struct tomoyo_request_info *r) |
| 970 | { |
| 971 | unsigned int count = 0; |
| 972 | struct tomoyo_domain_info *domain = r->domain; |
| 973 | struct tomoyo_acl_info *ptr; |
| 974 | |
| 975 | if (r->mode != TOMOYO_CONFIG_LEARNING) |
| 976 | return false; |
| 977 | if (!domain) |
| 978 | return true; |
| 979 | list_for_each_entry_rcu(ptr, &domain->acl_info_list, list) { |
Tetsuo Handa | 7c75964 | 2011-06-26 23:15:31 +0900 | [diff] [blame] | 980 | u16 perm; |
| 981 | u8 i; |
Tetsuo Handa | 7e3d199 | 2010-07-27 10:08:29 +0900 | [diff] [blame] | 982 | if (ptr->is_deleted) |
Tetsuo Handa | 237ab45 | 2010-06-12 20:46:22 +0900 | [diff] [blame] | 983 | continue; |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 984 | switch (ptr->type) { |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 985 | case TOMOYO_TYPE_PATH_ACL: |
| 986 | perm = container_of(ptr, struct tomoyo_path_acl, head) |
| 987 | ->perm; |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 988 | break; |
| 989 | case TOMOYO_TYPE_PATH2_ACL: |
| 990 | perm = container_of(ptr, struct tomoyo_path2_acl, head) |
| 991 | ->perm; |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 992 | break; |
| 993 | case TOMOYO_TYPE_PATH_NUMBER_ACL: |
| 994 | perm = container_of(ptr, struct tomoyo_path_number_acl, |
| 995 | head)->perm; |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 996 | break; |
Tetsuo Handa | 7509315 | 2010-06-16 16:23:55 +0900 | [diff] [blame] | 997 | case TOMOYO_TYPE_MKDEV_ACL: |
| 998 | perm = container_of(ptr, struct tomoyo_mkdev_acl, |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 999 | head)->perm; |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 1000 | break; |
Tetsuo Handa | 237ab45 | 2010-06-12 20:46:22 +0900 | [diff] [blame] | 1001 | default: |
Tetsuo Handa | 7c75964 | 2011-06-26 23:15:31 +0900 | [diff] [blame] | 1002 | perm = 1; |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 1003 | } |
Tetsuo Handa | 7c75964 | 2011-06-26 23:15:31 +0900 | [diff] [blame] | 1004 | for (i = 0; i < 16; i++) |
| 1005 | if (perm & (1 << i)) |
| 1006 | count++; |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 1007 | } |
Tetsuo Handa | bd03a3e | 2011-06-26 23:19:52 +0900 | [diff] [blame] | 1008 | if (count < tomoyo_profile(domain->ns, domain->profile)-> |
Tetsuo Handa | d5ca172 | 2011-06-26 23:18:21 +0900 | [diff] [blame] | 1009 | pref[TOMOYO_PREF_MAX_LEARNING_ENTRY]) |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 1010 | return true; |
Tetsuo Handa | 2c47ab9 | 2011-06-26 23:21:19 +0900 | [diff] [blame] | 1011 | if (!domain->flags[TOMOYO_DIF_QUOTA_WARNED]) { |
| 1012 | domain->flags[TOMOYO_DIF_QUOTA_WARNED] = true; |
| 1013 | /* r->granted = false; */ |
| 1014 | tomoyo_write_log(r, "%s", tomoyo_dif[TOMOYO_DIF_QUOTA_WARNED]); |
| 1015 | printk(KERN_WARNING "WARNING: " |
Tetsuo Handa | d5ca172 | 2011-06-26 23:18:21 +0900 | [diff] [blame] | 1016 | "Domain '%s' has too many ACLs to hold. " |
Tetsuo Handa | c3ef150 | 2010-05-17 10:12:46 +0900 | [diff] [blame] | 1017 | "Stopped learning mode.\n", domain->domainname->name); |
| 1018 | } |
| 1019 | return false; |
| 1020 | } |