tty_audit.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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/tty.h>
  13. struct tty_audit_buf {
  14. atomic_t count;
  15. struct mutex mutex; /* Protects all data below */
  16. int major, minor; /* The TTY which the data is from */
  17. unsigned icanon:1;
  18. size_t valid;
  19. unsigned char *data; /* Allocated size N_TTY_BUF_SIZE */
  20. };
  21. static struct tty_audit_buf *tty_audit_buf_alloc(int major, int minor,
  22. int icanon)
  23. {
  24. struct tty_audit_buf *buf;
  25. buf = kmalloc(sizeof(*buf), GFP_KERNEL);
  26. if (!buf)
  27. goto err;
  28. if (PAGE_SIZE != N_TTY_BUF_SIZE)
  29. buf->data = kmalloc(N_TTY_BUF_SIZE, GFP_KERNEL);
  30. else
  31. buf->data = (unsigned char *)__get_free_page(GFP_KERNEL);
  32. if (!buf->data)
  33. goto err_buf;
  34. atomic_set(&buf->count, 1);
  35. mutex_init(&buf->mutex);
  36. buf->major = major;
  37. buf->minor = minor;
  38. buf->icanon = icanon;
  39. buf->valid = 0;
  40. return buf;
  41. err_buf:
  42. kfree(buf);
  43. err:
  44. return NULL;
  45. }
  46. static void tty_audit_buf_free(struct tty_audit_buf *buf)
  47. {
  48. WARN_ON(buf->valid != 0);
  49. if (PAGE_SIZE != N_TTY_BUF_SIZE)
  50. kfree(buf->data);
  51. else
  52. free_page((unsigned long)buf->data);
  53. kfree(buf);
  54. }
  55. static void tty_audit_buf_put(struct tty_audit_buf *buf)
  56. {
  57. if (atomic_dec_and_test(&buf->count))
  58. tty_audit_buf_free(buf);
  59. }
  60. static void tty_audit_log(const char *description, struct task_struct *tsk,
  61. uid_t loginuid, unsigned sessionid, int major,
  62. int minor, unsigned char *data, size_t size)
  63. {
  64. struct audit_buffer *ab;
  65. ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_TTY);
  66. if (ab) {
  67. char name[sizeof(tsk->comm)];
  68. uid_t uid = task_uid(tsk);
  69. audit_log_format(ab, "%s pid=%u uid=%u auid=%u ses=%u "
  70. "major=%d minor=%d comm=", description,
  71. tsk->pid, uid, loginuid, sessionid,
  72. major, minor);
  73. get_task_comm(name, tsk);
  74. audit_log_untrustedstring(ab, name);
  75. audit_log_format(ab, " data=");
  76. audit_log_n_hex(ab, data, size);
  77. audit_log_end(ab);
  78. }
  79. }
  80. /**
  81. * tty_audit_buf_push - Push buffered data out
  82. *
  83. * Generate an audit message from the contents of @buf, which is owned by
  84. * @tsk with @loginuid. @buf->mutex must be locked.
  85. */
  86. static void tty_audit_buf_push(struct task_struct *tsk, uid_t loginuid,
  87. unsigned int sessionid,
  88. struct tty_audit_buf *buf)
  89. {
  90. if (buf->valid == 0)
  91. return;
  92. if (audit_enabled == 0)
  93. return;
  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. uid_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. sig->tty_audit_buf = NULL;
  141. }
  142. /**
  143. * tty_audit_tiocsti - Log TIOCSTI
  144. */
  145. void tty_audit_tiocsti(struct tty_struct *tty, char ch)
  146. {
  147. struct tty_audit_buf *buf;
  148. int major, minor, should_audit;
  149. spin_lock_irq(&current->sighand->siglock);
  150. should_audit = current->signal->audit_tty;
  151. buf = current->signal->tty_audit_buf;
  152. if (buf)
  153. atomic_inc(&buf->count);
  154. spin_unlock_irq(&current->sighand->siglock);
  155. major = tty->driver->major;
  156. minor = tty->driver->minor_start + tty->index;
  157. if (buf) {
  158. mutex_lock(&buf->mutex);
  159. if (buf->major == major && buf->minor == minor)
  160. tty_audit_buf_push_current(buf);
  161. mutex_unlock(&buf->mutex);
  162. tty_audit_buf_put(buf);
  163. }
  164. if (should_audit && audit_enabled) {
  165. uid_t auid;
  166. unsigned int sessionid;
  167. auid = audit_get_loginuid(current);
  168. sessionid = audit_get_sessionid(current);
  169. tty_audit_log("ioctl=TIOCSTI", current, auid, sessionid, major,
  170. minor, &ch, 1);
  171. }
  172. }
  173. /**
  174. * tty_audit_push_task - Flush task's pending audit data
  175. */
  176. void tty_audit_push_task(struct task_struct *tsk, uid_t loginuid, u32 sessionid)
  177. {
  178. struct tty_audit_buf *buf;
  179. spin_lock_irq(&tsk->sighand->siglock);
  180. buf = tsk->signal->tty_audit_buf;
  181. if (buf)
  182. atomic_inc(&buf->count);
  183. spin_unlock_irq(&tsk->sighand->siglock);
  184. if (!buf)
  185. return;
  186. mutex_lock(&buf->mutex);
  187. tty_audit_buf_push(tsk, loginuid, sessionid, buf);
  188. mutex_unlock(&buf->mutex);
  189. tty_audit_buf_put(buf);
  190. }
  191. /**
  192. * tty_audit_buf_get - Get an audit buffer.
  193. *
  194. * Get an audit buffer for @tty, allocate it if necessary. Return %NULL
  195. * if TTY auditing is disabled or out of memory. Otherwise, return a new
  196. * reference to the buffer.
  197. */
  198. static struct tty_audit_buf *tty_audit_buf_get(struct tty_struct *tty)
  199. {
  200. struct tty_audit_buf *buf, *buf2;
  201. buf = NULL;
  202. buf2 = NULL;
  203. spin_lock_irq(&current->sighand->siglock);
  204. if (likely(!current->signal->audit_tty))
  205. goto out;
  206. buf = current->signal->tty_audit_buf;
  207. if (buf) {
  208. atomic_inc(&buf->count);
  209. goto out;
  210. }
  211. spin_unlock_irq(&current->sighand->siglock);
  212. buf2 = tty_audit_buf_alloc(tty->driver->major,
  213. tty->driver->minor_start + tty->index,
  214. tty->icanon);
  215. if (buf2 == NULL) {
  216. audit_log_lost("out of memory in TTY auditing");
  217. return NULL;
  218. }
  219. spin_lock_irq(&current->sighand->siglock);
  220. if (!current->signal->audit_tty)
  221. goto out;
  222. buf = current->signal->tty_audit_buf;
  223. if (!buf) {
  224. current->signal->tty_audit_buf = buf2;
  225. buf = buf2;
  226. buf2 = NULL;
  227. }
  228. atomic_inc(&buf->count);
  229. /* Fall through */
  230. out:
  231. spin_unlock_irq(&current->sighand->siglock);
  232. if (buf2)
  233. tty_audit_buf_free(buf2);
  234. return buf;
  235. }
  236. /**
  237. * tty_audit_add_data - Add data for TTY auditing.
  238. *
  239. * Audit @data of @size from @tty, if necessary.
  240. */
  241. void tty_audit_add_data(struct tty_struct *tty, unsigned char *data,
  242. size_t size)
  243. {
  244. struct tty_audit_buf *buf;
  245. int major, minor;
  246. if (unlikely(size == 0))
  247. return;
  248. if (tty->driver->type == TTY_DRIVER_TYPE_PTY
  249. && tty->driver->subtype == PTY_TYPE_MASTER)
  250. return;
  251. buf = tty_audit_buf_get(tty);
  252. if (!buf)
  253. return;
  254. mutex_lock(&buf->mutex);
  255. major = tty->driver->major;
  256. minor = tty->driver->minor_start + tty->index;
  257. if (buf->major != major || buf->minor != minor
  258. || buf->icanon != tty->icanon) {
  259. tty_audit_buf_push_current(buf);
  260. buf->major = major;
  261. buf->minor = minor;
  262. buf->icanon = tty->icanon;
  263. }
  264. do {
  265. size_t run;
  266. run = N_TTY_BUF_SIZE - buf->valid;
  267. if (run > size)
  268. run = size;
  269. memcpy(buf->data + buf->valid, data, run);
  270. buf->valid += run;
  271. data += run;
  272. size -= run;
  273. if (buf->valid == N_TTY_BUF_SIZE)
  274. tty_audit_buf_push_current(buf);
  275. } while (size != 0);
  276. mutex_unlock(&buf->mutex);
  277. tty_audit_buf_put(buf);
  278. }
  279. /**
  280. * tty_audit_push - Push buffered data out
  281. *
  282. * Make sure no audit data is pending for @tty on the current process.
  283. */
  284. void tty_audit_push(struct tty_struct *tty)
  285. {
  286. struct tty_audit_buf *buf;
  287. spin_lock_irq(&current->sighand->siglock);
  288. if (likely(!current->signal->audit_tty)) {
  289. spin_unlock_irq(&current->sighand->siglock);
  290. return;
  291. }
  292. buf = current->signal->tty_audit_buf;
  293. if (buf)
  294. atomic_inc(&buf->count);
  295. spin_unlock_irq(&current->sighand->siglock);
  296. if (buf) {
  297. int major, minor;
  298. major = tty->driver->major;
  299. minor = tty->driver->minor_start + tty->index;
  300. mutex_lock(&buf->mutex);
  301. if (buf->major == major && buf->minor == minor)
  302. tty_audit_buf_push_current(buf);
  303. mutex_unlock(&buf->mutex);
  304. tty_audit_buf_put(buf);
  305. }
  306. }