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