Commit | Line | Data |
---|---|---|
522ed776 MT |
1 | /* |
2 | * Creating audit events from TTY input. | |
3 | * | |
4 | * Copyright (C) 2007 Red Hat, Inc. All rights reserved. This copyrighted | |
5 | * material is made available to anyone wishing to use, modify, copy, or | |
6 | * redistribute it subject to the terms and conditions of the GNU General | |
7 | * Public License v.2. | |
8 | * | |
9 | * Authors: Miloslav Trmac <mitr@redhat.com> | |
10 | */ | |
11 | ||
12 | #include <linux/audit.h> | |
5a0e3ad6 | 13 | #include <linux/slab.h> |
522ed776 MT |
14 | #include <linux/tty.h> |
15 | ||
16 | struct tty_audit_buf { | |
522ed776 | 17 | struct mutex mutex; /* Protects all data below */ |
4d240b64 | 18 | dev_t dev; /* The TTY which the data is from */ |
522ed776 MT |
19 | unsigned icanon:1; |
20 | size_t valid; | |
21 | unsigned char *data; /* Allocated size N_TTY_BUF_SIZE */ | |
22 | }; | |
23 | ||
55b6314a PH |
24 | static struct tty_audit_buf *tty_audit_buf_ref(void) |
25 | { | |
26 | struct tty_audit_buf *buf; | |
27 | ||
28 | buf = current->signal->tty_audit_buf; | |
29 | WARN_ON(buf == ERR_PTR(-ESRCH)); | |
30 | return buf; | |
31 | } | |
32 | ||
a75c9b09 | 33 | static struct tty_audit_buf *tty_audit_buf_alloc(void) |
522ed776 MT |
34 | { |
35 | struct tty_audit_buf *buf; | |
36 | ||
66c6ceae | 37 | buf = kmalloc(sizeof(*buf), GFP_KERNEL); |
522ed776 MT |
38 | if (!buf) |
39 | goto err; | |
c481c707 | 40 | buf->data = kmalloc(N_TTY_BUF_SIZE, GFP_KERNEL); |
522ed776 MT |
41 | if (!buf->data) |
42 | goto err_buf; | |
522ed776 | 43 | mutex_init(&buf->mutex); |
4d240b64 | 44 | buf->dev = MKDEV(0, 0); |
a75c9b09 | 45 | buf->icanon = 0; |
522ed776 MT |
46 | buf->valid = 0; |
47 | return buf; | |
48 | ||
49 | err_buf: | |
50 | kfree(buf); | |
51 | err: | |
52 | return NULL; | |
53 | } | |
54 | ||
55 | static void tty_audit_buf_free(struct tty_audit_buf *buf) | |
56 | { | |
57 | WARN_ON(buf->valid != 0); | |
c481c707 | 58 | kfree(buf->data); |
522ed776 MT |
59 | kfree(buf); |
60 | } | |
61 | ||
4d240b64 | 62 | static void tty_audit_log(const char *description, dev_t dev, |
152f497b | 63 | unsigned char *data, size_t size) |
522ed776 MT |
64 | { |
65 | struct audit_buffer *ab; | |
152f497b | 66 | struct task_struct *tsk = current; |
f1dc4867 | 67 | pid_t pid = task_pid_nr(tsk); |
152f497b EP |
68 | uid_t uid = from_kuid(&init_user_ns, task_uid(tsk)); |
69 | uid_t loginuid = from_kuid(&init_user_ns, audit_get_loginuid(tsk)); | |
4440e854 | 70 | unsigned int sessionid = audit_get_sessionid(tsk); |
522ed776 | 71 | |
522ed776 MT |
72 | ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_TTY); |
73 | if (ab) { | |
74 | char name[sizeof(tsk->comm)]; | |
152f497b EP |
75 | |
76 | audit_log_format(ab, "%s pid=%u uid=%u auid=%u ses=%u major=%d" | |
f1dc4867 | 77 | " minor=%d comm=", description, pid, uid, |
4d240b64 | 78 | loginuid, sessionid, MAJOR(dev), MINOR(dev)); |
522ed776 MT |
79 | get_task_comm(name, tsk); |
80 | audit_log_untrustedstring(ab, name); | |
81 | audit_log_format(ab, " data="); | |
1e641743 | 82 | audit_log_n_hex(ab, data, size); |
522ed776 MT |
83 | audit_log_end(ab); |
84 | } | |
1e641743 AV |
85 | } |
86 | ||
87 | /** | |
88 | * tty_audit_buf_push - Push buffered data out | |
89 | * | |
90 | * Generate an audit message from the contents of @buf, which is owned by | |
152f497b | 91 | * the current task. @buf->mutex must be locked. |
1e641743 | 92 | */ |
152f497b | 93 | static void tty_audit_buf_push(struct tty_audit_buf *buf) |
1e641743 AV |
94 | { |
95 | if (buf->valid == 0) | |
96 | return; | |
00bff392 XF |
97 | if (audit_enabled == 0) { |
98 | buf->valid = 0; | |
1e641743 | 99 | return; |
00bff392 | 100 | } |
4d240b64 | 101 | tty_audit_log("tty", buf->dev, buf->data, buf->valid); |
522ed776 MT |
102 | buf->valid = 0; |
103 | } | |
104 | ||
522ed776 MT |
105 | /** |
106 | * tty_audit_exit - Handle a task exit | |
107 | * | |
108 | * Make sure all buffered data is written out and deallocate the buffer. | |
109 | * Only needs to be called if current->signal->tty_audit_buf != %NULL. | |
5493090f PH |
110 | * |
111 | * The process is single-threaded at this point; no other threads share | |
112 | * current->signal. | |
522ed776 MT |
113 | */ |
114 | void tty_audit_exit(void) | |
115 | { | |
116 | struct tty_audit_buf *buf; | |
117 | ||
55b6314a | 118 | buf = xchg(¤t->signal->tty_audit_buf, ERR_PTR(-ESRCH)); |
522ed776 MT |
119 | if (!buf) |
120 | return; | |
121 | ||
152f497b | 122 | tty_audit_buf_push(buf); |
5493090f | 123 | tty_audit_buf_free(buf); |
522ed776 MT |
124 | } |
125 | ||
126 | /** | |
127 | * tty_audit_fork - Copy TTY audit state for a new task | |
128 | * | |
129 | * Set up TTY audit state in @sig from current. @sig needs no locking. | |
130 | */ | |
131 | void tty_audit_fork(struct signal_struct *sig) | |
132 | { | |
522ed776 | 133 | sig->audit_tty = current->signal->audit_tty; |
522ed776 MT |
134 | } |
135 | ||
1e641743 AV |
136 | /** |
137 | * tty_audit_tiocsti - Log TIOCSTI | |
138 | */ | |
139 | void tty_audit_tiocsti(struct tty_struct *tty, char ch) | |
140 | { | |
4d240b64 | 141 | dev_t dev; |
1e641743 | 142 | |
4d240b64 | 143 | dev = MKDEV(tty->driver->major, tty->driver->minor_start) + tty->index; |
82b5c93a PH |
144 | if (tty_audit_push()) |
145 | return; | |
1e641743 | 146 | |
54555919 | 147 | if (audit_enabled) |
4d240b64 | 148 | tty_audit_log("ioctl=TIOCSTI", dev, &ch, 1); |
1e641743 AV |
149 | } |
150 | ||
522ed776 | 151 | /** |
37282a77 | 152 | * tty_audit_push - Flush current's pending audit data |
3c80fe4a | 153 | * |
37282a77 | 154 | * Returns 0 if success, -EPERM if tty audit is disabled |
522ed776 | 155 | */ |
37282a77 | 156 | int tty_audit_push(void) |
522ed776 | 157 | { |
2e28d38a | 158 | struct tty_audit_buf *buf; |
522ed776 | 159 | |
2e28d38a PH |
160 | if (~current->signal->audit_tty & AUDIT_TTY_ENABLE) |
161 | return -EPERM; | |
162 | ||
55b6314a PH |
163 | buf = tty_audit_buf_ref(); |
164 | if (!IS_ERR_OR_NULL(buf)) { | |
2e28d38a PH |
165 | mutex_lock(&buf->mutex); |
166 | tty_audit_buf_push(buf); | |
167 | mutex_unlock(&buf->mutex); | |
2e28d38a | 168 | } |
3c80fe4a | 169 | return 0; |
522ed776 MT |
170 | } |
171 | ||
172 | /** | |
173 | * tty_audit_buf_get - Get an audit buffer. | |
174 | * | |
a75c9b09 | 175 | * Get an audit buffer, allocate it if necessary. Return %NULL |
55b6314a PH |
176 | * if out of memory or ERR_PTR(-ESRCH) if tty_audit_exit() has already |
177 | * occurred. Otherwise, return a new reference to the buffer. | |
522ed776 | 178 | */ |
a75c9b09 | 179 | static struct tty_audit_buf *tty_audit_buf_get(void) |
522ed776 | 180 | { |
fbaa1227 | 181 | struct tty_audit_buf *buf; |
522ed776 | 182 | |
55b6314a | 183 | buf = tty_audit_buf_ref(); |
5493090f PH |
184 | if (buf) |
185 | return buf; | |
522ed776 | 186 | |
fbaa1227 PH |
187 | buf = tty_audit_buf_alloc(); |
188 | if (buf == NULL) { | |
522ed776 MT |
189 | audit_log_lost("out of memory in TTY auditing"); |
190 | return NULL; | |
191 | } | |
192 | ||
fbaa1227 PH |
193 | /* Race to use this buffer, free it if another wins */ |
194 | if (cmpxchg(¤t->signal->tty_audit_buf, NULL, buf) != NULL) | |
195 | tty_audit_buf_free(buf); | |
55b6314a | 196 | return tty_audit_buf_ref(); |
522ed776 MT |
197 | } |
198 | ||
199 | /** | |
200 | * tty_audit_add_data - Add data for TTY auditing. | |
201 | * | |
202 | * Audit @data of @size from @tty, if necessary. | |
203 | */ | |
309426ae | 204 | void tty_audit_add_data(struct tty_struct *tty, const void *data, size_t size) |
522ed776 MT |
205 | { |
206 | struct tty_audit_buf *buf; | |
309426ae | 207 | unsigned int icanon = !!L_ICANON(tty); |
2e28d38a | 208 | unsigned int audit_tty; |
4d240b64 | 209 | dev_t dev; |
522ed776 | 210 | |
f17c3662 PH |
211 | audit_tty = READ_ONCE(current->signal->audit_tty); |
212 | if (~audit_tty & AUDIT_TTY_ENABLE) | |
213 | return; | |
214 | ||
522ed776 MT |
215 | if (unlikely(size == 0)) |
216 | return; | |
217 | ||
d7c0ba40 PH |
218 | if (tty->driver->type == TTY_DRIVER_TYPE_PTY |
219 | && tty->driver->subtype == PTY_TYPE_MASTER) | |
220 | return; | |
221 | ||
2e28d38a | 222 | if ((~audit_tty & AUDIT_TTY_LOG_PASSWD) && icanon && !L_ECHO(tty)) |
46e959ea RGB |
223 | return; |
224 | ||
a75c9b09 | 225 | buf = tty_audit_buf_get(); |
55b6314a | 226 | if (IS_ERR_OR_NULL(buf)) |
522ed776 MT |
227 | return; |
228 | ||
229 | mutex_lock(&buf->mutex); | |
4d240b64 PH |
230 | dev = MKDEV(tty->driver->major, tty->driver->minor_start) + tty->index; |
231 | if (buf->dev != dev || buf->icanon != icanon) { | |
152f497b | 232 | tty_audit_buf_push(buf); |
4d240b64 | 233 | buf->dev = dev; |
6c633f27 | 234 | buf->icanon = icanon; |
522ed776 MT |
235 | } |
236 | do { | |
237 | size_t run; | |
238 | ||
239 | run = N_TTY_BUF_SIZE - buf->valid; | |
240 | if (run > size) | |
241 | run = size; | |
242 | memcpy(buf->data + buf->valid, data, run); | |
243 | buf->valid += run; | |
244 | data += run; | |
245 | size -= run; | |
246 | if (buf->valid == N_TTY_BUF_SIZE) | |
152f497b | 247 | tty_audit_buf_push(buf); |
522ed776 MT |
248 | } while (size != 0); |
249 | mutex_unlock(&buf->mutex); | |
522ed776 | 250 | } |