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/file.h>
14 #include <linux/fdtable.h>
15 #include <linux/tty.h>
17 struct tty_audit_buf
{
19 struct mutex mutex
; /* Protects all data below */
20 int major
, minor
; /* The TTY which the data is from */
23 unsigned char *data
; /* Allocated size N_TTY_BUF_SIZE */
26 static struct tty_audit_buf
*tty_audit_buf_alloc(int major
, int minor
,
29 struct tty_audit_buf
*buf
;
31 buf
= kmalloc(sizeof(*buf
), GFP_KERNEL
);
34 if (PAGE_SIZE
!= N_TTY_BUF_SIZE
)
35 buf
->data
= kmalloc(N_TTY_BUF_SIZE
, GFP_KERNEL
);
37 buf
->data
= (unsigned char *)__get_free_page(GFP_KERNEL
);
40 atomic_set(&buf
->count
, 1);
41 mutex_init(&buf
->mutex
);
54 static void tty_audit_buf_free(struct tty_audit_buf
*buf
)
56 WARN_ON(buf
->valid
!= 0);
57 if (PAGE_SIZE
!= N_TTY_BUF_SIZE
)
60 free_page((unsigned long)buf
->data
);
64 static void tty_audit_buf_put(struct tty_audit_buf
*buf
)
66 if (atomic_dec_and_test(&buf
->count
))
67 tty_audit_buf_free(buf
);
71 * tty_audit_buf_push - Push buffered data out
73 * Generate an audit message from the contents of @buf, which is owned by
74 * @tsk with @loginuid. @buf->mutex must be locked.
76 static void tty_audit_buf_push(struct task_struct
*tsk
, uid_t loginuid
,
77 unsigned int sessionid
,
78 struct tty_audit_buf
*buf
)
80 struct audit_buffer
*ab
;
84 if (audit_enabled
== 0)
86 ab
= audit_log_start(NULL
, GFP_KERNEL
, AUDIT_TTY
);
88 char name
[sizeof(tsk
->comm
)];
90 audit_log_format(ab
, "tty pid=%u uid=%u auid=%u ses=%u "
91 "major=%d minor=%d comm=", tsk
->pid
, tsk
->uid
,
92 loginuid
, sessionid
, buf
->major
, buf
->minor
);
93 get_task_comm(name
, tsk
);
94 audit_log_untrustedstring(ab
, name
);
95 audit_log_format(ab
, " data=");
96 audit_log_n_untrustedstring(ab
, buf
->data
, buf
->valid
);
103 * tty_audit_buf_push_current - Push buffered data out
105 * Generate an audit message from the contents of @buf, which is owned by
106 * the current task. @buf->mutex must be locked.
108 static void tty_audit_buf_push_current(struct tty_audit_buf
*buf
)
110 uid_t auid
= audit_get_loginuid(current
);
111 unsigned int sessionid
= audit_get_sessionid(current
);
112 tty_audit_buf_push(current
, auid
, sessionid
, buf
);
116 * tty_audit_exit - Handle a task exit
118 * Make sure all buffered data is written out and deallocate the buffer.
119 * Only needs to be called if current->signal->tty_audit_buf != %NULL.
121 void tty_audit_exit(void)
123 struct tty_audit_buf
*buf
;
125 spin_lock_irq(¤t
->sighand
->siglock
);
126 buf
= current
->signal
->tty_audit_buf
;
127 current
->signal
->tty_audit_buf
= NULL
;
128 spin_unlock_irq(¤t
->sighand
->siglock
);
132 mutex_lock(&buf
->mutex
);
133 tty_audit_buf_push_current(buf
);
134 mutex_unlock(&buf
->mutex
);
136 tty_audit_buf_put(buf
);
140 * tty_audit_fork - Copy TTY audit state for a new task
142 * Set up TTY audit state in @sig from current. @sig needs no locking.
144 void tty_audit_fork(struct signal_struct
*sig
)
146 spin_lock_irq(¤t
->sighand
->siglock
);
147 sig
->audit_tty
= current
->signal
->audit_tty
;
148 spin_unlock_irq(¤t
->sighand
->siglock
);
149 sig
->tty_audit_buf
= NULL
;
153 * tty_audit_push_task - Flush task's pending audit data
155 void tty_audit_push_task(struct task_struct
*tsk
, uid_t loginuid
, u32 sessionid
)
157 struct tty_audit_buf
*buf
;
159 spin_lock_irq(&tsk
->sighand
->siglock
);
160 buf
= tsk
->signal
->tty_audit_buf
;
162 atomic_inc(&buf
->count
);
163 spin_unlock_irq(&tsk
->sighand
->siglock
);
167 mutex_lock(&buf
->mutex
);
168 tty_audit_buf_push(tsk
, loginuid
, sessionid
, buf
);
169 mutex_unlock(&buf
->mutex
);
171 tty_audit_buf_put(buf
);
175 * tty_audit_buf_get - Get an audit buffer.
177 * Get an audit buffer for @tty, allocate it if necessary. Return %NULL
178 * if TTY auditing is disabled or out of memory. Otherwise, return a new
179 * reference to the buffer.
181 static struct tty_audit_buf
*tty_audit_buf_get(struct tty_struct
*tty
)
183 struct tty_audit_buf
*buf
, *buf2
;
187 spin_lock_irq(¤t
->sighand
->siglock
);
188 if (likely(!current
->signal
->audit_tty
))
190 buf
= current
->signal
->tty_audit_buf
;
192 atomic_inc(&buf
->count
);
195 spin_unlock_irq(¤t
->sighand
->siglock
);
197 buf2
= tty_audit_buf_alloc(tty
->driver
->major
,
198 tty
->driver
->minor_start
+ tty
->index
,
201 audit_log_lost("out of memory in TTY auditing");
205 spin_lock_irq(¤t
->sighand
->siglock
);
206 if (!current
->signal
->audit_tty
)
208 buf
= current
->signal
->tty_audit_buf
;
210 current
->signal
->tty_audit_buf
= buf2
;
214 atomic_inc(&buf
->count
);
217 spin_unlock_irq(¤t
->sighand
->siglock
);
219 tty_audit_buf_free(buf2
);
224 * tty_audit_add_data - Add data for TTY auditing.
226 * Audit @data of @size from @tty, if necessary.
228 void tty_audit_add_data(struct tty_struct
*tty
, unsigned char *data
,
231 struct tty_audit_buf
*buf
;
234 if (unlikely(size
== 0))
237 if (tty
->driver
->type
== TTY_DRIVER_TYPE_PTY
238 && tty
->driver
->subtype
== PTY_TYPE_MASTER
)
241 buf
= tty_audit_buf_get(tty
);
245 mutex_lock(&buf
->mutex
);
246 major
= tty
->driver
->major
;
247 minor
= tty
->driver
->minor_start
+ tty
->index
;
248 if (buf
->major
!= major
|| buf
->minor
!= minor
249 || buf
->icanon
!= tty
->icanon
) {
250 tty_audit_buf_push_current(buf
);
253 buf
->icanon
= tty
->icanon
;
258 run
= N_TTY_BUF_SIZE
- buf
->valid
;
261 memcpy(buf
->data
+ buf
->valid
, data
, run
);
265 if (buf
->valid
== N_TTY_BUF_SIZE
)
266 tty_audit_buf_push_current(buf
);
268 mutex_unlock(&buf
->mutex
);
269 tty_audit_buf_put(buf
);
273 * tty_audit_push - Push buffered data out
275 * Make sure no audit data is pending for @tty on the current process.
277 void tty_audit_push(struct tty_struct
*tty
)
279 struct tty_audit_buf
*buf
;
281 spin_lock_irq(¤t
->sighand
->siglock
);
282 if (likely(!current
->signal
->audit_tty
)) {
283 spin_unlock_irq(¤t
->sighand
->siglock
);
286 buf
= current
->signal
->tty_audit_buf
;
288 atomic_inc(&buf
->count
);
289 spin_unlock_irq(¤t
->sighand
->siglock
);
294 major
= tty
->driver
->major
;
295 minor
= tty
->driver
->minor_start
+ tty
->index
;
296 mutex_lock(&buf
->mutex
);
297 if (buf
->major
== major
&& buf
->minor
== minor
)
298 tty_audit_buf_push_current(buf
);
299 mutex_unlock(&buf
->mutex
);
300 tty_audit_buf_put(buf
);