Blame view

drivers/tty/tty_audit.c 8.55 KB
522ed7767   Miloslav Trmac   Audit: add TTY in...
1
2
3
4
5
6
7
8
9
10
11
12
  /*
   * Creating audit events from TTY input.
   *
   * Copyright (C) 2007 Red Hat, Inc.  All rights reserved.  This copyrighted
   * material is made available to anyone wishing to use, modify, copy, or
   * redistribute it subject to the terms and conditions of the GNU General
   * Public License v.2.
   *
   * Authors: Miloslav Trmac <mitr@redhat.com>
   */
  
  #include <linux/audit.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
13
  #include <linux/slab.h>
522ed7767   Miloslav Trmac   Audit: add TTY in...
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
  #include <linux/tty.h>
  
  struct tty_audit_buf {
  	atomic_t count;
  	struct mutex mutex;	/* Protects all data below */
  	int major, minor;	/* The TTY which the data is from */
  	unsigned icanon:1;
  	size_t valid;
  	unsigned char *data;	/* Allocated size N_TTY_BUF_SIZE */
  };
  
  static struct tty_audit_buf *tty_audit_buf_alloc(int major, int minor,
  						 int icanon)
  {
  	struct tty_audit_buf *buf;
66c6ceae3   Alan Cox   tty_audit: fix ch...
29
  	buf = kmalloc(sizeof(*buf), GFP_KERNEL);
522ed7767   Miloslav Trmac   Audit: add TTY in...
30
31
  	if (!buf)
  		goto err;
c481c707f   Alan Cox   tty: remove buffe...
32
  	buf->data = kmalloc(N_TTY_BUF_SIZE, GFP_KERNEL);
522ed7767   Miloslav Trmac   Audit: add TTY in...
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
  	if (!buf->data)
  		goto err_buf;
  	atomic_set(&buf->count, 1);
  	mutex_init(&buf->mutex);
  	buf->major = major;
  	buf->minor = minor;
  	buf->icanon = icanon;
  	buf->valid = 0;
  	return buf;
  
  err_buf:
  	kfree(buf);
  err:
  	return NULL;
  }
  
  static void tty_audit_buf_free(struct tty_audit_buf *buf)
  {
  	WARN_ON(buf->valid != 0);
c481c707f   Alan Cox   tty: remove buffe...
52
  	kfree(buf->data);
522ed7767   Miloslav Trmac   Audit: add TTY in...
53
54
55
56
57
58
59
60
  	kfree(buf);
  }
  
  static void tty_audit_buf_put(struct tty_audit_buf *buf)
  {
  	if (atomic_dec_and_test(&buf->count))
  		tty_audit_buf_free(buf);
  }
1e641743f   Al Viro   Audit: Log TIOCSTI
61
62
63
  static void tty_audit_log(const char *description, struct task_struct *tsk,
  			  uid_t loginuid, unsigned sessionid, int major,
  			  int minor, unsigned char *data, size_t size)
522ed7767   Miloslav Trmac   Audit: add TTY in...
64
65
  {
  	struct audit_buffer *ab;
522ed7767   Miloslav Trmac   Audit: add TTY in...
66
67
68
  	ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_TTY);
  	if (ab) {
  		char name[sizeof(tsk->comm)];
66303bce9   David Howells   CRED: Wrap task c...
69
  		uid_t uid = task_uid(tsk);
522ed7767   Miloslav Trmac   Audit: add TTY in...
70

1e641743f   Al Viro   Audit: Log TIOCSTI
71
72
  		audit_log_format(ab, "%s pid=%u uid=%u auid=%u ses=%u "
  				 "major=%d minor=%d comm=", description,
66303bce9   David Howells   CRED: Wrap task c...
73
  				 tsk->pid, uid, loginuid, sessionid,
1e641743f   Al Viro   Audit: Log TIOCSTI
74
  				 major, minor);
522ed7767   Miloslav Trmac   Audit: add TTY in...
75
76
77
  		get_task_comm(name, tsk);
  		audit_log_untrustedstring(ab, name);
  		audit_log_format(ab, " data=");
1e641743f   Al Viro   Audit: Log TIOCSTI
78
  		audit_log_n_hex(ab, data, size);
522ed7767   Miloslav Trmac   Audit: add TTY in...
79
80
  		audit_log_end(ab);
  	}
1e641743f   Al Viro   Audit: Log TIOCSTI
81
82
83
84
85
86
87
88
89
90
91
92
93
94
  }
  
  /**
   *	tty_audit_buf_push	-	Push buffered data out
   *
   *	Generate an audit message from the contents of @buf, which is owned by
   *	@tsk with @loginuid.  @buf->mutex must be locked.
   */
  static void tty_audit_buf_push(struct task_struct *tsk, uid_t loginuid,
  			       unsigned int sessionid,
  			       struct tty_audit_buf *buf)
  {
  	if (buf->valid == 0)
  		return;
00bff392c   Xiaotian Feng   tty_audit: fix tt...
95
96
  	if (audit_enabled == 0) {
  		buf->valid = 0;
1e641743f   Al Viro   Audit: Log TIOCSTI
97
  		return;
00bff392c   Xiaotian Feng   tty_audit: fix tt...
98
  	}
1e641743f   Al Viro   Audit: Log TIOCSTI
99
100
  	tty_audit_log("tty", tsk, loginuid, sessionid, buf->major, buf->minor,
  		      buf->data, buf->valid);
522ed7767   Miloslav Trmac   Audit: add TTY in...
101
102
103
104
105
106
107
108
109
110
111
  	buf->valid = 0;
  }
  
  /**
   *	tty_audit_buf_push_current	-	Push buffered data out
   *
   *	Generate an audit message from the contents of @buf, which is owned by
   *	the current task.  @buf->mutex must be locked.
   */
  static void tty_audit_buf_push_current(struct tty_audit_buf *buf)
  {
4746ec5b0   Eric Paris   [AUDIT] add sessi...
112
113
114
  	uid_t auid = audit_get_loginuid(current);
  	unsigned int sessionid = audit_get_sessionid(current);
  	tty_audit_buf_push(current, auid, sessionid, buf);
522ed7767   Miloslav Trmac   Audit: add TTY in...
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
  }
  
  /**
   *	tty_audit_exit	-	Handle a task exit
   *
   *	Make sure all buffered data is written out and deallocate the buffer.
   *	Only needs to be called if current->signal->tty_audit_buf != %NULL.
   */
  void tty_audit_exit(void)
  {
  	struct tty_audit_buf *buf;
  
  	spin_lock_irq(&current->sighand->siglock);
  	buf = current->signal->tty_audit_buf;
  	current->signal->tty_audit_buf = NULL;
  	spin_unlock_irq(&current->sighand->siglock);
  	if (!buf)
  		return;
  
  	mutex_lock(&buf->mutex);
  	tty_audit_buf_push_current(buf);
  	mutex_unlock(&buf->mutex);
  
  	tty_audit_buf_put(buf);
  }
  
  /**
   *	tty_audit_fork	-	Copy TTY audit state for a new task
   *
   *	Set up TTY audit state in @sig from current.  @sig needs no locking.
   */
  void tty_audit_fork(struct signal_struct *sig)
  {
  	spin_lock_irq(&current->sighand->siglock);
  	sig->audit_tty = current->signal->audit_tty;
  	spin_unlock_irq(&current->sighand->siglock);
522ed7767   Miloslav Trmac   Audit: add TTY in...
151
152
153
  }
  
  /**
1e641743f   Al Viro   Audit: Log TIOCSTI
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
   *	tty_audit_tiocsti	-	Log TIOCSTI
   */
  void tty_audit_tiocsti(struct tty_struct *tty, char ch)
  {
  	struct tty_audit_buf *buf;
  	int major, minor, should_audit;
  
  	spin_lock_irq(&current->sighand->siglock);
  	should_audit = current->signal->audit_tty;
  	buf = current->signal->tty_audit_buf;
  	if (buf)
  		atomic_inc(&buf->count);
  	spin_unlock_irq(&current->sighand->siglock);
  
  	major = tty->driver->major;
  	minor = tty->driver->minor_start + tty->index;
  	if (buf) {
  		mutex_lock(&buf->mutex);
  		if (buf->major == major && buf->minor == minor)
  			tty_audit_buf_push_current(buf);
  		mutex_unlock(&buf->mutex);
  		tty_audit_buf_put(buf);
  	}
  
  	if (should_audit && audit_enabled) {
  		uid_t auid;
  		unsigned int sessionid;
  
  		auid = audit_get_loginuid(current);
  		sessionid = audit_get_sessionid(current);
  		tty_audit_log("ioctl=TIOCSTI", current, auid, sessionid, major,
  			      minor, &ch, 1);
  	}
  }
  
  /**
3c80fe4ac   Thomas Gleixner   audit: Call tty_a...
190
191
192
193
194
195
196
197
   * tty_audit_push_task	-	Flush task's pending audit data
   * @tsk:		task pointer
   * @loginuid:		sender login uid
   * @sessionid:		sender session id
   *
   * Called with a ref on @tsk held. Try to lock sighand and get a
   * reference to the tty audit buffer if available.
   * Flush the buffer or return an appropriate error code.
522ed7767   Miloslav Trmac   Audit: add TTY in...
198
   */
3c80fe4ac   Thomas Gleixner   audit: Call tty_a...
199
  int tty_audit_push_task(struct task_struct *tsk, uid_t loginuid, u32 sessionid)
522ed7767   Miloslav Trmac   Audit: add TTY in...
200
  {
3c80fe4ac   Thomas Gleixner   audit: Call tty_a...
201
202
  	struct tty_audit_buf *buf = ERR_PTR(-EPERM);
  	unsigned long flags;
522ed7767   Miloslav Trmac   Audit: add TTY in...
203

3c80fe4ac   Thomas Gleixner   audit: Call tty_a...
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
  	if (!lock_task_sighand(tsk, &flags))
  		return -ESRCH;
  
  	if (tsk->signal->audit_tty) {
  		buf = tsk->signal->tty_audit_buf;
  		if (buf)
  			atomic_inc(&buf->count);
  	}
  	unlock_task_sighand(tsk, &flags);
  
  	/*
  	 * Return 0 when signal->audit_tty set
  	 * but tsk->signal->tty_audit_buf == NULL.
  	 */
  	if (!buf || IS_ERR(buf))
  		return PTR_ERR(buf);
522ed7767   Miloslav Trmac   Audit: add TTY in...
220
221
  
  	mutex_lock(&buf->mutex);
4746ec5b0   Eric Paris   [AUDIT] add sessi...
222
  	tty_audit_buf_push(tsk, loginuid, sessionid, buf);
522ed7767   Miloslav Trmac   Audit: add TTY in...
223
224
225
  	mutex_unlock(&buf->mutex);
  
  	tty_audit_buf_put(buf);
3c80fe4ac   Thomas Gleixner   audit: Call tty_a...
226
  	return 0;
522ed7767   Miloslav Trmac   Audit: add TTY in...
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
  }
  
  /**
   *	tty_audit_buf_get	-	Get an audit buffer.
   *
   *	Get an audit buffer for @tty, allocate it if necessary.  Return %NULL
   *	if TTY auditing is disabled or out of memory.  Otherwise, return a new
   *	reference to the buffer.
   */
  static struct tty_audit_buf *tty_audit_buf_get(struct tty_struct *tty)
  {
  	struct tty_audit_buf *buf, *buf2;
  
  	buf = NULL;
  	buf2 = NULL;
  	spin_lock_irq(&current->sighand->siglock);
  	if (likely(!current->signal->audit_tty))
  		goto out;
  	buf = current->signal->tty_audit_buf;
  	if (buf) {
  		atomic_inc(&buf->count);
  		goto out;
  	}
  	spin_unlock_irq(&current->sighand->siglock);
  
  	buf2 = tty_audit_buf_alloc(tty->driver->major,
  				   tty->driver->minor_start + tty->index,
  				   tty->icanon);
  	if (buf2 == NULL) {
  		audit_log_lost("out of memory in TTY auditing");
  		return NULL;
  	}
  
  	spin_lock_irq(&current->sighand->siglock);
  	if (!current->signal->audit_tty)
  		goto out;
  	buf = current->signal->tty_audit_buf;
  	if (!buf) {
  		current->signal->tty_audit_buf = buf2;
  		buf = buf2;
  		buf2 = NULL;
  	}
  	atomic_inc(&buf->count);
  	/* Fall through */
   out:
  	spin_unlock_irq(&current->sighand->siglock);
  	if (buf2)
  		tty_audit_buf_free(buf2);
  	return buf;
  }
  
  /**
   *	tty_audit_add_data	-	Add data for TTY auditing.
   *
   *	Audit @data of @size from @tty, if necessary.
   */
  void tty_audit_add_data(struct tty_struct *tty, unsigned char *data,
  			size_t size)
  {
  	struct tty_audit_buf *buf;
  	int major, minor;
  
  	if (unlikely(size == 0))
  		return;
41126226e   Miloslav Trmac   [patch 1/2] audit...
291
292
293
  	if (tty->driver->type == TTY_DRIVER_TYPE_PTY
  	    && tty->driver->subtype == PTY_TYPE_MASTER)
  		return;
522ed7767   Miloslav Trmac   Audit: add TTY in...
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
  	buf = tty_audit_buf_get(tty);
  	if (!buf)
  		return;
  
  	mutex_lock(&buf->mutex);
  	major = tty->driver->major;
  	minor = tty->driver->minor_start + tty->index;
  	if (buf->major != major || buf->minor != minor
  	    || buf->icanon != tty->icanon) {
  		tty_audit_buf_push_current(buf);
  		buf->major = major;
  		buf->minor = minor;
  		buf->icanon = tty->icanon;
  	}
  	do {
  		size_t run;
  
  		run = N_TTY_BUF_SIZE - buf->valid;
  		if (run > size)
  			run = size;
  		memcpy(buf->data + buf->valid, data, run);
  		buf->valid += run;
  		data += run;
  		size -= run;
  		if (buf->valid == N_TTY_BUF_SIZE)
  			tty_audit_buf_push_current(buf);
  	} while (size != 0);
  	mutex_unlock(&buf->mutex);
  	tty_audit_buf_put(buf);
  }
  
  /**
   *	tty_audit_push	-	Push buffered data out
   *
   *	Make sure no audit data is pending for @tty on the current process.
   */
  void tty_audit_push(struct tty_struct *tty)
  {
  	struct tty_audit_buf *buf;
  
  	spin_lock_irq(&current->sighand->siglock);
  	if (likely(!current->signal->audit_tty)) {
  		spin_unlock_irq(&current->sighand->siglock);
  		return;
  	}
  	buf = current->signal->tty_audit_buf;
  	if (buf)
  		atomic_inc(&buf->count);
  	spin_unlock_irq(&current->sighand->siglock);
  
  	if (buf) {
  		int major, minor;
  
  		major = tty->driver->major;
  		minor = tty->driver->minor_start + tty->index;
  		mutex_lock(&buf->mutex);
  		if (buf->major == major && buf->minor == minor)
  			tty_audit_buf_push_current(buf);
  		mutex_unlock(&buf->mutex);
  		tty_audit_buf_put(buf);
  	}
  }