2 * Creating audit events from TTY input.
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
9 * Authors: Miloslav Trmac <mitr@redhat.com>
12 #include <linux/audit.h>
13 #include <linux/tty.h>
15 struct tty_audit_buf
{
17 struct mutex mutex
; /* Protects all data below */
18 int major
, minor
; /* The TTY which the data is from */
21 unsigned char *data
; /* Allocated size N_TTY_BUF_SIZE */
24 static struct tty_audit_buf
*tty_audit_buf_alloc(int major
, int minor
,
27 struct tty_audit_buf
*buf
;
29 buf
= kmalloc(sizeof(*buf
), GFP_KERNEL
);
32 buf
->data
= kmalloc(N_TTY_BUF_SIZE
, GFP_KERNEL
);
35 atomic_set(&buf
->count
, 1);
36 mutex_init(&buf
->mutex
);
49 static void tty_audit_buf_free(struct tty_audit_buf
*buf
)
51 WARN_ON(buf
->valid
!= 0);
56 static void tty_audit_buf_put(struct tty_audit_buf
*buf
)
58 if (atomic_dec_and_test(&buf
->count
))
59 tty_audit_buf_free(buf
);
62 static void tty_audit_log(const char *description
, struct task_struct
*tsk
,
63 uid_t loginuid
, unsigned sessionid
, int major
,
64 int minor
, unsigned char *data
, size_t size
)
66 struct audit_buffer
*ab
;
68 ab
= audit_log_start(NULL
, GFP_KERNEL
, AUDIT_TTY
);
70 char name
[sizeof(tsk
->comm
)];
71 uid_t uid
= task_uid(tsk
);
73 audit_log_format(ab
, "%s pid=%u uid=%u auid=%u ses=%u "
74 "major=%d minor=%d comm=", description
,
75 tsk
->pid
, uid
, loginuid
, sessionid
,
77 get_task_comm(name
, tsk
);
78 audit_log_untrustedstring(ab
, name
);
79 audit_log_format(ab
, " data=");
80 audit_log_n_hex(ab
, data
, size
);
86 * tty_audit_buf_push - Push buffered data out
88 * Generate an audit message from the contents of @buf, which is owned by
89 * @tsk with @loginuid. @buf->mutex must be locked.
91 static void tty_audit_buf_push(struct task_struct
*tsk
, uid_t loginuid
,
92 unsigned int sessionid
,
93 struct tty_audit_buf
*buf
)
97 if (audit_enabled
== 0) {
101 tty_audit_log("tty", tsk
, loginuid
, sessionid
, buf
->major
, buf
->minor
,
102 buf
->data
, buf
->valid
);
107 * tty_audit_buf_push_current - Push buffered data out
109 * Generate an audit message from the contents of @buf, which is owned by
110 * the current task. @buf->mutex must be locked.
112 static void tty_audit_buf_push_current(struct tty_audit_buf
*buf
)
114 uid_t auid
= audit_get_loginuid(current
);
115 unsigned int sessionid
= audit_get_sessionid(current
);
116 tty_audit_buf_push(current
, auid
, sessionid
, buf
);
120 * tty_audit_exit - Handle a task exit
122 * Make sure all buffered data is written out and deallocate the buffer.
123 * Only needs to be called if current->signal->tty_audit_buf != %NULL.
125 void tty_audit_exit(void)
127 struct tty_audit_buf
*buf
;
129 spin_lock_irq(¤t
->sighand
->siglock
);
130 buf
= current
->signal
->tty_audit_buf
;
131 current
->signal
->tty_audit_buf
= NULL
;
132 spin_unlock_irq(¤t
->sighand
->siglock
);
136 mutex_lock(&buf
->mutex
);
137 tty_audit_buf_push_current(buf
);
138 mutex_unlock(&buf
->mutex
);
140 tty_audit_buf_put(buf
);
144 * tty_audit_fork - Copy TTY audit state for a new task
146 * Set up TTY audit state in @sig from current. @sig needs no locking.
148 void tty_audit_fork(struct signal_struct
*sig
)
150 spin_lock_irq(¤t
->sighand
->siglock
);
151 sig
->audit_tty
= current
->signal
->audit_tty
;
152 spin_unlock_irq(¤t
->sighand
->siglock
);
153 sig
->tty_audit_buf
= NULL
;
157 * tty_audit_tiocsti - Log TIOCSTI
159 void tty_audit_tiocsti(struct tty_struct
*tty
, char ch
)
161 struct tty_audit_buf
*buf
;
162 int major
, minor
, should_audit
;
164 spin_lock_irq(¤t
->sighand
->siglock
);
165 should_audit
= current
->signal
->audit_tty
;
166 buf
= current
->signal
->tty_audit_buf
;
168 atomic_inc(&buf
->count
);
169 spin_unlock_irq(¤t
->sighand
->siglock
);
171 major
= tty
->driver
->major
;
172 minor
= tty
->driver
->minor_start
+ tty
->index
;
174 mutex_lock(&buf
->mutex
);
175 if (buf
->major
== major
&& buf
->minor
== minor
)
176 tty_audit_buf_push_current(buf
);
177 mutex_unlock(&buf
->mutex
);
178 tty_audit_buf_put(buf
);
181 if (should_audit
&& audit_enabled
) {
183 unsigned int sessionid
;
185 auid
= audit_get_loginuid(current
);
186 sessionid
= audit_get_sessionid(current
);
187 tty_audit_log("ioctl=TIOCSTI", current
, auid
, sessionid
, major
,
193 * tty_audit_push_task - Flush task's pending audit data
195 void tty_audit_push_task(struct task_struct
*tsk
, uid_t loginuid
, u32 sessionid
)
197 struct tty_audit_buf
*buf
;
199 spin_lock_irq(&tsk
->sighand
->siglock
);
200 buf
= tsk
->signal
->tty_audit_buf
;
202 atomic_inc(&buf
->count
);
203 spin_unlock_irq(&tsk
->sighand
->siglock
);
207 mutex_lock(&buf
->mutex
);
208 tty_audit_buf_push(tsk
, loginuid
, sessionid
, buf
);
209 mutex_unlock(&buf
->mutex
);
211 tty_audit_buf_put(buf
);
215 * tty_audit_buf_get - Get an audit buffer.
217 * Get an audit buffer for @tty, allocate it if necessary. Return %NULL
218 * if TTY auditing is disabled or out of memory. Otherwise, return a new
219 * reference to the buffer.
221 static struct tty_audit_buf
*tty_audit_buf_get(struct tty_struct
*tty
)
223 struct tty_audit_buf
*buf
, *buf2
;
227 spin_lock_irq(¤t
->sighand
->siglock
);
228 if (likely(!current
->signal
->audit_tty
))
230 buf
= current
->signal
->tty_audit_buf
;
232 atomic_inc(&buf
->count
);
235 spin_unlock_irq(¤t
->sighand
->siglock
);
237 buf2
= tty_audit_buf_alloc(tty
->driver
->major
,
238 tty
->driver
->minor_start
+ tty
->index
,
241 audit_log_lost("out of memory in TTY auditing");
245 spin_lock_irq(¤t
->sighand
->siglock
);
246 if (!current
->signal
->audit_tty
)
248 buf
= current
->signal
->tty_audit_buf
;
250 current
->signal
->tty_audit_buf
= buf2
;
254 atomic_inc(&buf
->count
);
257 spin_unlock_irq(¤t
->sighand
->siglock
);
259 tty_audit_buf_free(buf2
);
264 * tty_audit_add_data - Add data for TTY auditing.
266 * Audit @data of @size from @tty, if necessary.
268 void tty_audit_add_data(struct tty_struct
*tty
, unsigned char *data
,
271 struct tty_audit_buf
*buf
;
274 if (unlikely(size
== 0))
277 if (tty
->driver
->type
== TTY_DRIVER_TYPE_PTY
278 && tty
->driver
->subtype
== PTY_TYPE_MASTER
)
281 buf
= tty_audit_buf_get(tty
);
285 mutex_lock(&buf
->mutex
);
286 major
= tty
->driver
->major
;
287 minor
= tty
->driver
->minor_start
+ tty
->index
;
288 if (buf
->major
!= major
|| buf
->minor
!= minor
289 || buf
->icanon
!= tty
->icanon
) {
290 tty_audit_buf_push_current(buf
);
293 buf
->icanon
= tty
->icanon
;
298 run
= N_TTY_BUF_SIZE
- buf
->valid
;
301 memcpy(buf
->data
+ buf
->valid
, data
, run
);
305 if (buf
->valid
== N_TTY_BUF_SIZE
)
306 tty_audit_buf_push_current(buf
);
308 mutex_unlock(&buf
->mutex
);
309 tty_audit_buf_put(buf
);
313 * tty_audit_push - Push buffered data out
315 * Make sure no audit data is pending for @tty on the current process.
317 void tty_audit_push(struct tty_struct
*tty
)
319 struct tty_audit_buf
*buf
;
321 spin_lock_irq(¤t
->sighand
->siglock
);
322 if (likely(!current
->signal
->audit_tty
)) {
323 spin_unlock_irq(¤t
->sighand
->siglock
);
326 buf
= current
->signal
->tty_audit_buf
;
328 atomic_inc(&buf
->count
);
329 spin_unlock_irq(¤t
->sighand
->siglock
);
334 major
= tty
->driver
->major
;
335 minor
= tty
->driver
->minor_start
+ tty
->index
;
336 mutex_lock(&buf
->mutex
);
337 if (buf
->major
== major
&& buf
->minor
== minor
)
338 tty_audit_buf_push_current(buf
);
339 mutex_unlock(&buf
->mutex
);
340 tty_audit_buf_put(buf
);