tty_audit.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. #include <linux/audit.h>
  12. #include <linux/slab.h>
  13. #include <linux/tty.h>
  14. struct tty_audit_buf {
  15. atomic_t count;
  16. struct mutex mutex; /* Protects all data below */
  17. int major, minor; /* The TTY which the data is from */
  18. unsigned icanon:1;
  19. size_t valid;
  20. unsigned char *data; /* Allocated size N_TTY_BUF_SIZE */
  21. };
  22. static struct tty_audit_buf *tty_audit_buf_alloc(int major, int minor,
  23. unsigned icanon)
  24. {
  25. struct tty_audit_buf *buf;
  26. buf = kmalloc(sizeof(*buf), GFP_KERNEL);
  27. if (!buf)
  28. goto err;
  29. buf->data = kmalloc(N_TTY_BUF_SIZE, GFP_KERNEL);
  30. if (!buf->data)
  31. goto err_buf;
  32. atomic_set(&buf->count, 1);
  33. mutex_init(&buf->mutex);
  34. buf->major = major;
  35. buf->minor = minor;
  36. buf->icanon = icanon;
  37. buf->valid = 0;
  38. return buf;
  39. err_buf:
  40. kfree(buf);
  41. err:
  42. return NULL;
  43. }
  44. static void tty_audit_buf_free(struct tty_audit_buf *buf)
  45. {
  46. WARN_ON(buf->valid != 0);
  47. kfree(buf->data);
  48. kfree(buf);
  49. }
  50. static void tty_audit_buf_put(struct tty_audit_buf *buf)
  51. {
  52. if (atomic_dec_and_test(&buf->count))
  53. tty_audit_buf_free(buf);
  54. }
  55. static void tty_audit_log(const char *description, struct task_struct *tsk,
  56. kuid_t loginuid, unsigned sessionid, int major,
  57. int minor, unsigned char *data, size_t size)
  58. {
  59. struct audit_buffer *ab;
  60. ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_TTY);
  61. if (ab) {
  62. char name[sizeof(tsk->comm)];
  63. kuid_t uid = task_uid(tsk);
  64. audit_log_format(ab, "%s pid=%u uid=%u auid=%u ses=%u "
  65. "major=%d minor=%d comm=", description,
  66. tsk->pid,
  67. from_kuid(&init_user_ns, uid),
  68. from_kuid(&init_user_ns, loginuid),
  69. sessionid,
  70. major, minor);
  71. get_task_comm(name, tsk);
  72. audit_log_untrustedstring(ab, name);
  73. audit_log_format(ab, " data=");
  74. audit_log_n_hex(ab, data, size);
  75. audit_log_end(ab);
  76. }
  77. }
  78. /**
  79. * tty_audit_buf_push - Push buffered data out
  80. *
  81. * Generate an audit message from the contents of @buf, which is owned by
  82. * @tsk with @loginuid. @buf->mutex must be locked.
  83. */
  84. static void tty_audit_buf_push(struct task_struct *tsk, kuid_t loginuid,
  85. unsigned int sessionid,
  86. struct tty_audit_buf *buf)
  87. {
  88. if (buf->valid == 0)
  89. return;
  90. if (audit_enabled == 0) {
  91. buf->valid = 0;
  92. return;
  93. }
  94. tty_audit_log("tty", tsk, loginuid, sessionid, buf->major, buf->minor,
  95. buf->data, buf->valid);
  96. buf->valid = 0;
  97. }
  98. /**
  99. * tty_audit_buf_push_current - Push buffered data out
  100. *
  101. * Generate an audit message from the contents of @buf, which is owned by
  102. * the current task. @buf->mutex must be locked.
  103. */
  104. static void tty_audit_buf_push_current(struct tty_audit_buf *buf)
  105. {
  106. kuid_t auid = audit_get_loginuid(current);
  107. unsigned int sessionid = audit_get_sessionid(current);
  108. tty_audit_buf_push(current, auid, sessionid, buf);
  109. }
  110. /**
  111. * tty_audit_exit - Handle a task exit
  112. *
  113. * Make sure all buffered data is written out and deallocate the buffer.
  114. * Only needs to be called if current->signal->tty_audit_buf != %NULL.
  115. */
  116. void tty_audit_exit(void)
  117. {
  118. struct tty_audit_buf *buf;
  119. spin_lock_irq(&current->sighand->siglock);
  120. buf = current->signal->tty_audit_buf;
  121. current->signal->tty_audit_buf = NULL;
  122. spin_unlock_irq(&current->sighand->siglock);
  123. if (!buf)
  124. return;
  125. mutex_lock(&buf->mutex);
  126. tty_audit_buf_push_current(buf);
  127. mutex_unlock(&buf->mutex);
  128. tty_audit_buf_put(buf);
  129. }
  130. /**
  131. * tty_audit_fork - Copy TTY audit state for a new task
  132. *
  133. * Set up TTY audit state in @sig from current. @sig needs no locking.
  134. */
  135. void tty_audit_fork(struct signal_struct *sig)
  136. {
  137. spin_lock_irq(&current->sighand->siglock);
  138. sig->audit_tty = current->signal->audit_tty;
  139. spin_unlock_irq(&current->sighand->siglock);
  140. }
  141. /**
  142. * tty_audit_tiocsti - Log TIOCSTI
  143. */
  144. void tty_audit_tiocsti(struct tty_struct *tty, char ch)
  145. {
  146. struct tty_audit_buf *buf;
  147. int major, minor, should_audit;
  148. spin_lock_irq(&current->sighand->siglock);
  149. should_audit = current->signal->audit_tty;
  150. buf = current->signal->tty_audit_buf;
  151. if (buf)
  152. atomic_inc(&buf->count);
  153. spin_unlock_irq(&current->sighand->siglock);
  154. major = tty->driver->major;
  155. minor = tty->driver->minor_start + tty->index;
  156. if (buf) {
  157. mutex_lock(&buf->mutex);
  158. if (buf->major == major && buf->minor == minor)
  159. tty_audit_buf_push_current(buf);
  160. mutex_unlock(&buf->mutex);
  161. tty_audit_buf_put(buf);
  162. }
  163. if (should_audit && audit_enabled) {
  164. kuid_t auid;
  165. unsigned int sessionid;
  166. auid = audit_get_loginuid(current);
  167. sessionid = audit_get_sessionid(current);
  168. tty_audit_log("ioctl=TIOCSTI", current, auid, sessionid, major,
  169. minor, &ch, 1);
  170. }
  171. }
  172. /**
  173. * tty_audit_push_task - Flush task's pending audit data
  174. * @tsk: task pointer
  175. * @loginuid: sender login uid
  176. * @sessionid: sender session id
  177. *
  178. * Called with a ref on @tsk held. Try to lock sighand and get a
  179. * reference to the tty audit buffer if available.
  180. * Flush the buffer or return an appropriate error code.
  181. */
  182. int tty_audit_push_task(struct task_struct *tsk, kuid_t loginuid, u32 sessionid)
  183. {
  184. struct tty_audit_buf *buf = ERR_PTR(-EPERM);
  185. unsigned long flags;
  186. if (!lock_task_sighand(tsk, &flags))
  187. return -ESRCH;
  188. if (tsk->signal->audit_tty) {
  189. buf = tsk->signal->tty_audit_buf;
  190. if (buf)
  191. atomic_inc(&buf->count);
  192. }
  193. unlock_task_sighand(tsk, &flags);
  194. /*
  195. * Return 0 when signal->audit_tty set
  196. * but tsk->signal->tty_audit_buf == NULL.
  197. */
  198. if (!buf || IS_ERR(buf))
  199. return PTR_ERR(buf);
  200. mutex_lock(&buf->mutex);
  201. tty_audit_buf_push(tsk, loginuid, sessionid, buf);
  202. mutex_unlock(&buf->mutex);
  203. tty_audit_buf_put(buf);
  204. return 0;
  205. }
  206. /**
  207. * tty_audit_buf_get - Get an audit buffer.
  208. *
  209. * Get an audit buffer for @tty, allocate it if necessary. Return %NULL
  210. * if TTY auditing is disabled or out of memory. Otherwise, return a new
  211. * reference to the buffer.
  212. */
  213. static struct tty_audit_buf *tty_audit_buf_get(struct tty_struct *tty,
  214. unsigned icanon)
  215. {
  216. struct tty_audit_buf *buf, *buf2;
  217. buf = NULL;
  218. buf2 = NULL;
  219. spin_lock_irq(&current->sighand->siglock);
  220. if (likely(!current->signal->audit_tty))
  221. goto out;
  222. buf = current->signal->tty_audit_buf;
  223. if (buf) {
  224. atomic_inc(&buf->count);
  225. goto out;
  226. }
  227. spin_unlock_irq(&current->sighand->siglock);
  228. buf2 = tty_audit_buf_alloc(tty->driver->major,
  229. tty->driver->minor_start + tty->index,
  230. icanon);
  231. if (buf2 == NULL) {
  232. audit_log_lost("out of memory in TTY auditing");
  233. return NULL;
  234. }
  235. spin_lock_irq(&current->sighand->siglock);
  236. if (!current->signal->audit_tty)
  237. goto out;
  238. buf = current->signal->tty_audit_buf;
  239. if (!buf) {
  240. current->signal->tty_audit_buf = buf2;
  241. buf = buf2;
  242. buf2 = NULL;
  243. }
  244. atomic_inc(&buf->count);
  245. /* Fall through */
  246. out:
  247. spin_unlock_irq(&current->sighand->siglock);
  248. if (buf2)
  249. tty_audit_buf_free(buf2);
  250. return buf;
  251. }
  252. /**
  253. * tty_audit_add_data - Add data for TTY auditing.
  254. *
  255. * Audit @data of @size from @tty, if necessary.
  256. */
  257. void tty_audit_add_data(struct tty_struct *tty, unsigned char *data,
  258. size_t size, unsigned icanon)
  259. {
  260. struct tty_audit_buf *buf;
  261. int major, minor;
  262. if (unlikely(size == 0))
  263. return;
  264. if (tty->driver->type == TTY_DRIVER_TYPE_PTY
  265. && tty->driver->subtype == PTY_TYPE_MASTER)
  266. return;
  267. buf = tty_audit_buf_get(tty, icanon);
  268. if (!buf)
  269. return;
  270. mutex_lock(&buf->mutex);
  271. major = tty->driver->major;
  272. minor = tty->driver->minor_start + tty->index;
  273. if (buf->major != major || buf->minor != minor
  274. || buf->icanon != icanon) {
  275. tty_audit_buf_push_current(buf);
  276. buf->major = major;
  277. buf->minor = minor;
  278. buf->icanon = icanon;
  279. }
  280. do {
  281. size_t run;
  282. run = N_TTY_BUF_SIZE - buf->valid;
  283. if (run > size)
  284. run = size;
  285. memcpy(buf->data + buf->valid, data, run);
  286. buf->valid += run;
  287. data += run;
  288. size -= run;
  289. if (buf->valid == N_TTY_BUF_SIZE)
  290. tty_audit_buf_push_current(buf);
  291. } while (size != 0);
  292. mutex_unlock(&buf->mutex);
  293. tty_audit_buf_put(buf);
  294. }
  295. /**
  296. * tty_audit_push - Push buffered data out
  297. *
  298. * Make sure no audit data is pending for @tty on the current process.
  299. */
  300. void tty_audit_push(struct tty_struct *tty)
  301. {
  302. struct tty_audit_buf *buf;
  303. spin_lock_irq(&current->sighand->siglock);
  304. if (likely(!current->signal->audit_tty)) {
  305. spin_unlock_irq(&current->sighand->siglock);
  306. return;
  307. }
  308. buf = current->signal->tty_audit_buf;
  309. if (buf)
  310. atomic_inc(&buf->count);
  311. spin_unlock_irq(&current->sighand->siglock);
  312. if (buf) {
  313. int major, minor;
  314. major = tty->driver->major;
  315. minor = tty->driver->minor_start + tty->index;
  316. mutex_lock(&buf->mutex);
  317. if (buf->major == major && buf->minor == minor)
  318. tty_audit_buf_push_current(buf);
  319. mutex_unlock(&buf->mutex);
  320. tty_audit_buf_put(buf);
  321. }
  322. }