^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * security/tomoyo/audit.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2005-2011 NTT DATA CORPORATION
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include "common.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * tomoyo_print_bprm - Print "struct linux_binprm" for auditing.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * @bprm: Pointer to "struct linux_binprm".
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * @dump: Pointer to "struct tomoyo_page_dump".
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * Returns the contents of @bprm on success, NULL otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * This function uses kzalloc(), so caller must kfree() if this function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * didn't return NULL.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) static char *tomoyo_print_bprm(struct linux_binprm *bprm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) struct tomoyo_page_dump *dump)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) static const int tomoyo_buffer_len = 4096 * 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) char *buffer = kzalloc(tomoyo_buffer_len, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) char *cp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) char *last_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) unsigned long pos = bprm->p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) int offset = pos % PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) int argv_count = bprm->argc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) int envp_count = bprm->envc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) bool truncated = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) if (!buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) len = snprintf(buffer, tomoyo_buffer_len - 1, "argv[]={ ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) cp = buffer + len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) if (!argv_count) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) memmove(cp, "} envp[]={ ", 11);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) cp += 11;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) last_start = cp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) while (argv_count || envp_count) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) if (!tomoyo_dump_page(bprm, pos, dump))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) pos += PAGE_SIZE - offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) /* Read. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) while (offset < PAGE_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) const char *kaddr = dump->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) const unsigned char c = kaddr[offset++];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) if (cp == last_start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) *cp++ = '"';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) if (cp >= buffer + tomoyo_buffer_len - 32) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) /* Reserve some room for "..." string. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) truncated = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) } else if (c == '\\') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) *cp++ = '\\';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) *cp++ = '\\';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) } else if (c > ' ' && c < 127) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) *cp++ = c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) } else if (!c) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) *cp++ = '"';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) *cp++ = ' ';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) last_start = cp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) *cp++ = '\\';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) *cp++ = (c >> 6) + '0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) *cp++ = ((c >> 3) & 7) + '0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) *cp++ = (c & 7) + '0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) if (c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) if (argv_count) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) if (--argv_count == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) if (truncated) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) cp = last_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) memmove(cp, "... ", 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) cp += 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) memmove(cp, "} envp[]={ ", 11);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) cp += 11;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) last_start = cp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) truncated = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) } else if (envp_count) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) if (--envp_count == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) if (truncated) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) cp = last_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) memmove(cp, "... ", 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) cp += 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) if (!argv_count && !envp_count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) *cp++ = '}';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) *cp = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) return buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) snprintf(buffer, tomoyo_buffer_len - 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) "argv[]={ ... } envp[]= { ... }");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) return buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) * tomoyo_filetype - Get string representation of file type.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) * @mode: Mode value for stat().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) * Returns file type string.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) static inline const char *tomoyo_filetype(const umode_t mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) switch (mode & S_IFMT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) case S_IFREG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) case 0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) return tomoyo_condition_keyword[TOMOYO_TYPE_IS_FILE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) case S_IFDIR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) return tomoyo_condition_keyword[TOMOYO_TYPE_IS_DIRECTORY];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) case S_IFLNK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) return tomoyo_condition_keyword[TOMOYO_TYPE_IS_SYMLINK];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) case S_IFIFO:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) return tomoyo_condition_keyword[TOMOYO_TYPE_IS_FIFO];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) case S_IFSOCK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) return tomoyo_condition_keyword[TOMOYO_TYPE_IS_SOCKET];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) case S_IFBLK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) return tomoyo_condition_keyword[TOMOYO_TYPE_IS_BLOCK_DEV];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) case S_IFCHR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) return tomoyo_condition_keyword[TOMOYO_TYPE_IS_CHAR_DEV];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) return "unknown"; /* This should not happen. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) * tomoyo_print_header - Get header line of audit log.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) * @r: Pointer to "struct tomoyo_request_info".
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) * Returns string representation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) * This function uses kmalloc(), so caller must kfree() if this function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) * didn't return NULL.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) static char *tomoyo_print_header(struct tomoyo_request_info *r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) struct tomoyo_time stamp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) const pid_t gpid = task_pid_nr(current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) struct tomoyo_obj_info *obj = r->obj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) static const int tomoyo_buffer_len = 4096;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) char *buffer = kmalloc(tomoyo_buffer_len, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) int pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) u8 i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) if (!buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) tomoyo_convert_time(ktime_get_real_seconds(), &stamp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) pos = snprintf(buffer, tomoyo_buffer_len - 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) "#%04u/%02u/%02u %02u:%02u:%02u# profile=%u mode=%s granted=%s (global-pid=%u) task={ pid=%u ppid=%u uid=%u gid=%u euid=%u egid=%u suid=%u sgid=%u fsuid=%u fsgid=%u }",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) stamp.year, stamp.month, stamp.day, stamp.hour,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) stamp.min, stamp.sec, r->profile, tomoyo_mode[r->mode],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) tomoyo_yesno(r->granted), gpid, tomoyo_sys_getpid(),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) tomoyo_sys_getppid(),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) from_kuid(&init_user_ns, current_uid()),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) from_kgid(&init_user_ns, current_gid()),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) from_kuid(&init_user_ns, current_euid()),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) from_kgid(&init_user_ns, current_egid()),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) from_kuid(&init_user_ns, current_suid()),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) from_kgid(&init_user_ns, current_sgid()),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) from_kuid(&init_user_ns, current_fsuid()),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) from_kgid(&init_user_ns, current_fsgid()));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) if (!obj)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) goto no_obj_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) if (!obj->validate_done) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) tomoyo_get_attributes(obj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) obj->validate_done = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) for (i = 0; i < TOMOYO_MAX_PATH_STAT; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) struct tomoyo_mini_stat *stat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) unsigned int dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) umode_t mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) if (!obj->stat_valid[i])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) stat = &obj->stat[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) dev = stat->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) mode = stat->mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) if (i & 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) pos += snprintf(buffer + pos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) tomoyo_buffer_len - 1 - pos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) " path%u.parent={ uid=%u gid=%u ino=%lu perm=0%o }",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) (i >> 1) + 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) from_kuid(&init_user_ns, stat->uid),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) from_kgid(&init_user_ns, stat->gid),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) (unsigned long)stat->ino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) stat->mode & S_IALLUGO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) pos += snprintf(buffer + pos, tomoyo_buffer_len - 1 - pos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) " path%u={ uid=%u gid=%u ino=%lu major=%u minor=%u perm=0%o type=%s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) (i >> 1) + 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) from_kuid(&init_user_ns, stat->uid),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) from_kgid(&init_user_ns, stat->gid),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) (unsigned long)stat->ino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) MAJOR(dev), MINOR(dev),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) mode & S_IALLUGO, tomoyo_filetype(mode));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) if (S_ISCHR(mode) || S_ISBLK(mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) dev = stat->rdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) pos += snprintf(buffer + pos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) tomoyo_buffer_len - 1 - pos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) " dev_major=%u dev_minor=%u",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) MAJOR(dev), MINOR(dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) pos += snprintf(buffer + pos, tomoyo_buffer_len - 1 - pos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) " }");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) no_obj_info:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) if (pos < tomoyo_buffer_len - 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) return buffer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) kfree(buffer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) * tomoyo_init_log - Allocate buffer for audit logs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) * @r: Pointer to "struct tomoyo_request_info".
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) * @len: Buffer size needed for @fmt and @args.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) * @fmt: The printf()'s format string.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) * @args: va_list structure for @fmt.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) * Returns pointer to allocated memory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) * This function uses kzalloc(), so caller must kfree() if this function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) * didn't return NULL.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) char *tomoyo_init_log(struct tomoyo_request_info *r, int len, const char *fmt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) va_list args)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) char *buf = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) char *bprm_info = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) const char *header = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) char *realpath = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) const char *symlink = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) int pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) const char *domainname = r->domain->domainname->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) header = tomoyo_print_header(r);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) if (!header)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) /* +10 is for '\n' etc. and '\0'. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) len += strlen(domainname) + strlen(header) + 10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) if (r->ee) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) struct file *file = r->ee->bprm->file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) realpath = tomoyo_realpath_from_path(&file->f_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) bprm_info = tomoyo_print_bprm(r->ee->bprm, &r->ee->dump);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) if (!realpath || !bprm_info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) /* +80 is for " exec={ realpath=\"%s\" argc=%d envc=%d %s }" */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) len += strlen(realpath) + 80 + strlen(bprm_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) } else if (r->obj && r->obj->symlink_target) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) symlink = r->obj->symlink_target->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) /* +18 is for " symlink.target=\"%s\"" */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) len += 18 + strlen(symlink);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) len = tomoyo_round2(len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) buf = kzalloc(len, GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) if (!buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) len--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) pos = snprintf(buf, len, "%s", header);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) if (realpath) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) struct linux_binprm *bprm = r->ee->bprm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) pos += snprintf(buf + pos, len - pos,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) " exec={ realpath=\"%s\" argc=%d envc=%d %s }",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) realpath, bprm->argc, bprm->envc, bprm_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) } else if (symlink)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) pos += snprintf(buf + pos, len - pos, " symlink.target=\"%s\"",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) symlink);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) pos += snprintf(buf + pos, len - pos, "\n%s\n", domainname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) vsnprintf(buf + pos, len - pos, fmt, args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) kfree(realpath);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) kfree(bprm_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) kfree(header);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) return buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) /* Wait queue for /sys/kernel/security/tomoyo/audit. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) static DECLARE_WAIT_QUEUE_HEAD(tomoyo_log_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) /* Structure for audit log. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) struct tomoyo_log {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) struct list_head list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) char *log;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) int size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) /* The list for "struct tomoyo_log". */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) static LIST_HEAD(tomoyo_log);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) /* Lock for "struct list_head tomoyo_log". */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) static DEFINE_SPINLOCK(tomoyo_log_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) /* Length of "stuct list_head tomoyo_log". */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) static unsigned int tomoyo_log_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) * tomoyo_get_audit - Get audit mode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) * @ns: Pointer to "struct tomoyo_policy_namespace".
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) * @profile: Profile number.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) * @index: Index number of functionality.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) * @is_granted: True if granted log, false otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) * Returns true if this request should be audited, false otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) static bool tomoyo_get_audit(const struct tomoyo_policy_namespace *ns,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) const u8 profile, const u8 index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) const struct tomoyo_acl_info *matched_acl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) const bool is_granted)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) u8 mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) const u8 category = tomoyo_index2category[index] +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) TOMOYO_MAX_MAC_INDEX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) struct tomoyo_profile *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) if (!tomoyo_policy_loaded)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) p = tomoyo_profile(ns, profile);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) if (tomoyo_log_count >= p->pref[TOMOYO_PREF_MAX_AUDIT_LOG])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) if (is_granted && matched_acl && matched_acl->cond &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) matched_acl->cond->grant_log != TOMOYO_GRANTLOG_AUTO)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) return matched_acl->cond->grant_log == TOMOYO_GRANTLOG_YES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) mode = p->config[index];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) if (mode == TOMOYO_CONFIG_USE_DEFAULT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) mode = p->config[category];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) if (mode == TOMOYO_CONFIG_USE_DEFAULT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) mode = p->default_config;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) if (is_granted)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) return mode & TOMOYO_CONFIG_WANT_GRANT_LOG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) return mode & TOMOYO_CONFIG_WANT_REJECT_LOG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) * tomoyo_write_log2 - Write an audit log.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) * @r: Pointer to "struct tomoyo_request_info".
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) * @len: Buffer size needed for @fmt and @args.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) * @fmt: The printf()'s format string.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) * @args: va_list structure for @fmt.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) * Returns nothing.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) void tomoyo_write_log2(struct tomoyo_request_info *r, int len, const char *fmt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) va_list args)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) char *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) struct tomoyo_log *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) bool quota_exceeded = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) if (!tomoyo_get_audit(r->domain->ns, r->profile, r->type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) r->matched_acl, r->granted))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) buf = tomoyo_init_log(r, len, fmt, args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) if (!buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) entry = kzalloc(sizeof(*entry), GFP_NOFS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) if (!entry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) kfree(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) entry->log = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) len = tomoyo_round2(strlen(buf) + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) * The entry->size is used for memory quota checks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) * Don't go beyond strlen(entry->log).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) entry->size = len + tomoyo_round2(sizeof(*entry));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) spin_lock(&tomoyo_log_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) if (tomoyo_memory_quota[TOMOYO_MEMORY_AUDIT] &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) tomoyo_memory_used[TOMOYO_MEMORY_AUDIT] + entry->size >=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) tomoyo_memory_quota[TOMOYO_MEMORY_AUDIT]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) quota_exceeded = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) tomoyo_memory_used[TOMOYO_MEMORY_AUDIT] += entry->size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) list_add_tail(&entry->list, &tomoyo_log);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) tomoyo_log_count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) spin_unlock(&tomoyo_log_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) if (quota_exceeded) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) kfree(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) kfree(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) wake_up(&tomoyo_log_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) * tomoyo_write_log - Write an audit log.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) * @r: Pointer to "struct tomoyo_request_info".
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) * @fmt: The printf()'s format string, followed by parameters.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) * Returns nothing.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) void tomoyo_write_log(struct tomoyo_request_info *r, const char *fmt, ...)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) va_list args;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) va_start(args, fmt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) len = vsnprintf((char *) &len, 1, fmt, args) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) va_end(args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) va_start(args, fmt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) tomoyo_write_log2(r, len, fmt, args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) va_end(args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) * tomoyo_read_log - Read an audit log.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) * @head: Pointer to "struct tomoyo_io_buffer".
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) * Returns nothing.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) void tomoyo_read_log(struct tomoyo_io_buffer *head)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) struct tomoyo_log *ptr = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) if (head->r.w_pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) kfree(head->read_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) head->read_buf = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) spin_lock(&tomoyo_log_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) if (!list_empty(&tomoyo_log)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) ptr = list_entry(tomoyo_log.next, typeof(*ptr), list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) list_del(&ptr->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) tomoyo_log_count--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) tomoyo_memory_used[TOMOYO_MEMORY_AUDIT] -= ptr->size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) spin_unlock(&tomoyo_log_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) if (ptr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) head->read_buf = ptr->log;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) head->r.w[head->r.w_pos++] = head->read_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) kfree(ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) * tomoyo_poll_log - Wait for an audit log.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) * @file: Pointer to "struct file".
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) * @wait: Pointer to "poll_table". Maybe NULL.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) * Returns EPOLLIN | EPOLLRDNORM when ready to read an audit log.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) __poll_t tomoyo_poll_log(struct file *file, poll_table *wait)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) if (tomoyo_log_count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) return EPOLLIN | EPOLLRDNORM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) poll_wait(file, &tomoyo_log_wait, wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) if (tomoyo_log_count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) return EPOLLIN | EPOLLRDNORM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) }