auditsc.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074
  1. /* auditsc.c -- System-call auditing support
  2. * Handles all system-call specific auditing features.
  3. *
  4. * Copyright 2003-2004 Red Hat Inc., Durham, North Carolina.
  5. * All Rights Reserved.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. * Written by Rickard E. (Rik) Faith <faith@redhat.com>
  22. *
  23. * Many of the ideas implemented here are from Stephen C. Tweedie,
  24. * especially the idea of avoiding a copy by using getname.
  25. *
  26. * The method for actual interception of syscall entry and exit (not in
  27. * this file -- see entry.S) is based on a GPL'd patch written by
  28. * okir@suse.de and Copyright 2003 SuSE Linux AG.
  29. *
  30. */
  31. #include <linux/init.h>
  32. #include <asm/atomic.h>
  33. #include <asm/types.h>
  34. #include <linux/mm.h>
  35. #include <linux/module.h>
  36. #include <linux/audit.h>
  37. #include <linux/personality.h>
  38. #include <linux/time.h>
  39. #include <asm/unistd.h>
  40. /* 0 = no checking
  41. 1 = put_count checking
  42. 2 = verbose put_count checking
  43. */
  44. #define AUDIT_DEBUG 0
  45. /* No syscall auditing will take place unless audit_enabled != 0. */
  46. extern int audit_enabled;
  47. /* AUDIT_NAMES is the number of slots we reserve in the audit_context
  48. * for saving names from getname(). */
  49. #define AUDIT_NAMES 20
  50. /* AUDIT_NAMES_RESERVED is the number of slots we reserve in the
  51. * audit_context from being used for nameless inodes from
  52. * path_lookup. */
  53. #define AUDIT_NAMES_RESERVED 7
  54. /* At task start time, the audit_state is set in the audit_context using
  55. a per-task filter. At syscall entry, the audit_state is augmented by
  56. the syscall filter. */
  57. enum audit_state {
  58. AUDIT_DISABLED, /* Do not create per-task audit_context.
  59. * No syscall-specific audit records can
  60. * be generated. */
  61. AUDIT_SETUP_CONTEXT, /* Create the per-task audit_context,
  62. * but don't necessarily fill it in at
  63. * syscall entry time (i.e., filter
  64. * instead). */
  65. AUDIT_BUILD_CONTEXT, /* Create the per-task audit_context,
  66. * and always fill it in at syscall
  67. * entry time. This makes a full
  68. * syscall record available if some
  69. * other part of the kernel decides it
  70. * should be recorded. */
  71. AUDIT_RECORD_CONTEXT /* Create the per-task audit_context,
  72. * always fill it in at syscall entry
  73. * time, and always write out the audit
  74. * record at syscall exit time. */
  75. };
  76. /* When fs/namei.c:getname() is called, we store the pointer in name and
  77. * we don't let putname() free it (instead we free all of the saved
  78. * pointers at syscall exit time).
  79. *
  80. * Further, in fs/namei.c:path_lookup() we store the inode and device. */
  81. struct audit_names {
  82. const char *name;
  83. unsigned long ino;
  84. dev_t dev;
  85. umode_t mode;
  86. uid_t uid;
  87. gid_t gid;
  88. dev_t rdev;
  89. };
  90. struct audit_aux_data {
  91. struct audit_aux_data *next;
  92. int type;
  93. };
  94. #define AUDIT_AUX_IPCPERM 0
  95. struct audit_aux_data_ipcctl {
  96. struct audit_aux_data d;
  97. struct ipc_perm p;
  98. unsigned long qbytes;
  99. uid_t uid;
  100. gid_t gid;
  101. mode_t mode;
  102. };
  103. /* The per-task audit context. */
  104. struct audit_context {
  105. int in_syscall; /* 1 if task is in a syscall */
  106. enum audit_state state;
  107. unsigned int serial; /* serial number for record */
  108. struct timespec ctime; /* time of syscall entry */
  109. uid_t loginuid; /* login uid (identity) */
  110. int major; /* syscall number */
  111. unsigned long argv[4]; /* syscall arguments */
  112. int return_valid; /* return code is valid */
  113. long return_code;/* syscall return code */
  114. int auditable; /* 1 if record should be written */
  115. int name_count;
  116. struct audit_names names[AUDIT_NAMES];
  117. struct audit_context *previous; /* For nested syscalls */
  118. struct audit_aux_data *aux;
  119. /* Save things to print about task_struct */
  120. pid_t pid;
  121. uid_t uid, euid, suid, fsuid;
  122. gid_t gid, egid, sgid, fsgid;
  123. unsigned long personality;
  124. int arch;
  125. #if AUDIT_DEBUG
  126. int put_count;
  127. int ino_count;
  128. #endif
  129. };
  130. /* Public API */
  131. /* There are three lists of rules -- one to search at task creation
  132. * time, one to search at syscall entry time, and another to search at
  133. * syscall exit time. */
  134. static LIST_HEAD(audit_tsklist);
  135. static LIST_HEAD(audit_entlist);
  136. static LIST_HEAD(audit_extlist);
  137. struct audit_entry {
  138. struct list_head list;
  139. struct rcu_head rcu;
  140. struct audit_rule rule;
  141. };
  142. /* Check to see if two rules are identical. It is called from
  143. * audit_del_rule during AUDIT_DEL. */
  144. static int audit_compare_rule(struct audit_rule *a, struct audit_rule *b)
  145. {
  146. int i;
  147. if (a->flags != b->flags)
  148. return 1;
  149. if (a->action != b->action)
  150. return 1;
  151. if (a->field_count != b->field_count)
  152. return 1;
  153. for (i = 0; i < a->field_count; i++) {
  154. if (a->fields[i] != b->fields[i]
  155. || a->values[i] != b->values[i])
  156. return 1;
  157. }
  158. for (i = 0; i < AUDIT_BITMASK_SIZE; i++)
  159. if (a->mask[i] != b->mask[i])
  160. return 1;
  161. return 0;
  162. }
  163. /* Note that audit_add_rule and audit_del_rule are called via
  164. * audit_receive() in audit.c, and are protected by
  165. * audit_netlink_sem. */
  166. static inline int audit_add_rule(struct audit_entry *entry,
  167. struct list_head *list)
  168. {
  169. if (entry->rule.flags & AUDIT_PREPEND) {
  170. entry->rule.flags &= ~AUDIT_PREPEND;
  171. list_add_rcu(&entry->list, list);
  172. } else {
  173. list_add_tail_rcu(&entry->list, list);
  174. }
  175. return 0;
  176. }
  177. static void audit_free_rule(struct rcu_head *head)
  178. {
  179. struct audit_entry *e = container_of(head, struct audit_entry, rcu);
  180. kfree(e);
  181. }
  182. /* Note that audit_add_rule and audit_del_rule are called via
  183. * audit_receive() in audit.c, and are protected by
  184. * audit_netlink_sem. */
  185. static inline int audit_del_rule(struct audit_rule *rule,
  186. struct list_head *list)
  187. {
  188. struct audit_entry *e;
  189. /* Do not use the _rcu iterator here, since this is the only
  190. * deletion routine. */
  191. list_for_each_entry(e, list, list) {
  192. if (!audit_compare_rule(rule, &e->rule)) {
  193. list_del_rcu(&e->list);
  194. call_rcu(&e->rcu, audit_free_rule);
  195. return 0;
  196. }
  197. }
  198. return -EFAULT; /* No matching rule */
  199. }
  200. /* Copy rule from user-space to kernel-space. Called during
  201. * AUDIT_ADD. */
  202. static int audit_copy_rule(struct audit_rule *d, struct audit_rule *s)
  203. {
  204. int i;
  205. if (s->action != AUDIT_NEVER
  206. && s->action != AUDIT_POSSIBLE
  207. && s->action != AUDIT_ALWAYS)
  208. return -1;
  209. if (s->field_count < 0 || s->field_count > AUDIT_MAX_FIELDS)
  210. return -1;
  211. d->flags = s->flags;
  212. d->action = s->action;
  213. d->field_count = s->field_count;
  214. for (i = 0; i < d->field_count; i++) {
  215. d->fields[i] = s->fields[i];
  216. d->values[i] = s->values[i];
  217. }
  218. for (i = 0; i < AUDIT_BITMASK_SIZE; i++) d->mask[i] = s->mask[i];
  219. return 0;
  220. }
  221. int audit_receive_filter(int type, int pid, int uid, int seq, void *data,
  222. uid_t loginuid)
  223. {
  224. u32 flags;
  225. struct audit_entry *entry;
  226. int err = 0;
  227. switch (type) {
  228. case AUDIT_LIST:
  229. /* The *_rcu iterators not needed here because we are
  230. always called with audit_netlink_sem held. */
  231. list_for_each_entry(entry, &audit_tsklist, list)
  232. audit_send_reply(pid, seq, AUDIT_LIST, 0, 1,
  233. &entry->rule, sizeof(entry->rule));
  234. list_for_each_entry(entry, &audit_entlist, list)
  235. audit_send_reply(pid, seq, AUDIT_LIST, 0, 1,
  236. &entry->rule, sizeof(entry->rule));
  237. list_for_each_entry(entry, &audit_extlist, list)
  238. audit_send_reply(pid, seq, AUDIT_LIST, 0, 1,
  239. &entry->rule, sizeof(entry->rule));
  240. audit_send_reply(pid, seq, AUDIT_LIST, 1, 1, NULL, 0);
  241. break;
  242. case AUDIT_ADD:
  243. if (!(entry = kmalloc(sizeof(*entry), GFP_KERNEL)))
  244. return -ENOMEM;
  245. if (audit_copy_rule(&entry->rule, data)) {
  246. kfree(entry);
  247. return -EINVAL;
  248. }
  249. flags = entry->rule.flags;
  250. if (!err && (flags & AUDIT_PER_TASK))
  251. err = audit_add_rule(entry, &audit_tsklist);
  252. if (!err && (flags & AUDIT_AT_ENTRY))
  253. err = audit_add_rule(entry, &audit_entlist);
  254. if (!err && (flags & AUDIT_AT_EXIT))
  255. err = audit_add_rule(entry, &audit_extlist);
  256. audit_log(NULL, "auid %u added an audit rule\n", loginuid);
  257. break;
  258. case AUDIT_DEL:
  259. flags =((struct audit_rule *)data)->flags;
  260. if (!err && (flags & AUDIT_PER_TASK))
  261. err = audit_del_rule(data, &audit_tsklist);
  262. if (!err && (flags & AUDIT_AT_ENTRY))
  263. err = audit_del_rule(data, &audit_entlist);
  264. if (!err && (flags & AUDIT_AT_EXIT))
  265. err = audit_del_rule(data, &audit_extlist);
  266. audit_log(NULL, "auid %u removed an audit rule\n", loginuid);
  267. break;
  268. default:
  269. return -EINVAL;
  270. }
  271. return err;
  272. }
  273. /* Compare a task_struct with an audit_rule. Return 1 on match, 0
  274. * otherwise. */
  275. static int audit_filter_rules(struct task_struct *tsk,
  276. struct audit_rule *rule,
  277. struct audit_context *ctx,
  278. enum audit_state *state)
  279. {
  280. int i, j;
  281. for (i = 0; i < rule->field_count; i++) {
  282. u32 field = rule->fields[i] & ~AUDIT_NEGATE;
  283. u32 value = rule->values[i];
  284. int result = 0;
  285. switch (field) {
  286. case AUDIT_PID:
  287. result = (tsk->pid == value);
  288. break;
  289. case AUDIT_UID:
  290. result = (tsk->uid == value);
  291. break;
  292. case AUDIT_EUID:
  293. result = (tsk->euid == value);
  294. break;
  295. case AUDIT_SUID:
  296. result = (tsk->suid == value);
  297. break;
  298. case AUDIT_FSUID:
  299. result = (tsk->fsuid == value);
  300. break;
  301. case AUDIT_GID:
  302. result = (tsk->gid == value);
  303. break;
  304. case AUDIT_EGID:
  305. result = (tsk->egid == value);
  306. break;
  307. case AUDIT_SGID:
  308. result = (tsk->sgid == value);
  309. break;
  310. case AUDIT_FSGID:
  311. result = (tsk->fsgid == value);
  312. break;
  313. case AUDIT_PERS:
  314. result = (tsk->personality == value);
  315. break;
  316. case AUDIT_ARCH:
  317. if (ctx)
  318. result = (ctx->arch == value);
  319. break;
  320. case AUDIT_EXIT:
  321. if (ctx && ctx->return_valid)
  322. result = (ctx->return_code == value);
  323. break;
  324. case AUDIT_SUCCESS:
  325. if (ctx && ctx->return_valid)
  326. result = (ctx->return_valid == AUDITSC_SUCCESS);
  327. break;
  328. case AUDIT_DEVMAJOR:
  329. if (ctx) {
  330. for (j = 0; j < ctx->name_count; j++) {
  331. if (MAJOR(ctx->names[j].dev)==value) {
  332. ++result;
  333. break;
  334. }
  335. }
  336. }
  337. break;
  338. case AUDIT_DEVMINOR:
  339. if (ctx) {
  340. for (j = 0; j < ctx->name_count; j++) {
  341. if (MINOR(ctx->names[j].dev)==value) {
  342. ++result;
  343. break;
  344. }
  345. }
  346. }
  347. break;
  348. case AUDIT_INODE:
  349. if (ctx) {
  350. for (j = 0; j < ctx->name_count; j++) {
  351. if (ctx->names[j].ino == value) {
  352. ++result;
  353. break;
  354. }
  355. }
  356. }
  357. break;
  358. case AUDIT_LOGINUID:
  359. result = 0;
  360. if (ctx)
  361. result = (ctx->loginuid == value);
  362. break;
  363. case AUDIT_ARG0:
  364. case AUDIT_ARG1:
  365. case AUDIT_ARG2:
  366. case AUDIT_ARG3:
  367. if (ctx)
  368. result = (ctx->argv[field-AUDIT_ARG0]==value);
  369. break;
  370. }
  371. if (rule->fields[i] & AUDIT_NEGATE)
  372. result = !result;
  373. if (!result)
  374. return 0;
  375. }
  376. switch (rule->action) {
  377. case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
  378. case AUDIT_POSSIBLE: *state = AUDIT_BUILD_CONTEXT; break;
  379. case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
  380. }
  381. return 1;
  382. }
  383. /* At process creation time, we can determine if system-call auditing is
  384. * completely disabled for this task. Since we only have the task
  385. * structure at this point, we can only check uid and gid.
  386. */
  387. static enum audit_state audit_filter_task(struct task_struct *tsk)
  388. {
  389. struct audit_entry *e;
  390. enum audit_state state;
  391. rcu_read_lock();
  392. list_for_each_entry_rcu(e, &audit_tsklist, list) {
  393. if (audit_filter_rules(tsk, &e->rule, NULL, &state)) {
  394. rcu_read_unlock();
  395. return state;
  396. }
  397. }
  398. rcu_read_unlock();
  399. return AUDIT_BUILD_CONTEXT;
  400. }
  401. /* At syscall entry and exit time, this filter is called if the
  402. * audit_state is not low enough that auditing cannot take place, but is
  403. * also not high enough that we already know we have to write and audit
  404. * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT).
  405. */
  406. static enum audit_state audit_filter_syscall(struct task_struct *tsk,
  407. struct audit_context *ctx,
  408. struct list_head *list)
  409. {
  410. struct audit_entry *e;
  411. enum audit_state state;
  412. int word = AUDIT_WORD(ctx->major);
  413. int bit = AUDIT_BIT(ctx->major);
  414. rcu_read_lock();
  415. list_for_each_entry_rcu(e, list, list) {
  416. if ((e->rule.mask[word] & bit) == bit
  417. && audit_filter_rules(tsk, &e->rule, ctx, &state)) {
  418. rcu_read_unlock();
  419. return state;
  420. }
  421. }
  422. rcu_read_unlock();
  423. return AUDIT_BUILD_CONTEXT;
  424. }
  425. /* This should be called with task_lock() held. */
  426. static inline struct audit_context *audit_get_context(struct task_struct *tsk,
  427. int return_valid,
  428. int return_code)
  429. {
  430. struct audit_context *context = tsk->audit_context;
  431. if (likely(!context))
  432. return NULL;
  433. context->return_valid = return_valid;
  434. context->return_code = return_code;
  435. if (context->in_syscall && !context->auditable) {
  436. enum audit_state state;
  437. state = audit_filter_syscall(tsk, context, &audit_extlist);
  438. if (state == AUDIT_RECORD_CONTEXT)
  439. context->auditable = 1;
  440. }
  441. context->pid = tsk->pid;
  442. context->uid = tsk->uid;
  443. context->gid = tsk->gid;
  444. context->euid = tsk->euid;
  445. context->suid = tsk->suid;
  446. context->fsuid = tsk->fsuid;
  447. context->egid = tsk->egid;
  448. context->sgid = tsk->sgid;
  449. context->fsgid = tsk->fsgid;
  450. context->personality = tsk->personality;
  451. tsk->audit_context = NULL;
  452. return context;
  453. }
  454. static inline void audit_free_names(struct audit_context *context)
  455. {
  456. int i;
  457. #if AUDIT_DEBUG == 2
  458. if (context->auditable
  459. ||context->put_count + context->ino_count != context->name_count) {
  460. printk(KERN_ERR "audit.c:%d(:%d): major=%d in_syscall=%d"
  461. " name_count=%d put_count=%d"
  462. " ino_count=%d [NOT freeing]\n",
  463. __LINE__,
  464. context->serial, context->major, context->in_syscall,
  465. context->name_count, context->put_count,
  466. context->ino_count);
  467. for (i = 0; i < context->name_count; i++)
  468. printk(KERN_ERR "names[%d] = %p = %s\n", i,
  469. context->names[i].name,
  470. context->names[i].name);
  471. dump_stack();
  472. return;
  473. }
  474. #endif
  475. #if AUDIT_DEBUG
  476. context->put_count = 0;
  477. context->ino_count = 0;
  478. #endif
  479. for (i = 0; i < context->name_count; i++)
  480. if (context->names[i].name)
  481. __putname(context->names[i].name);
  482. context->name_count = 0;
  483. }
  484. static inline void audit_free_aux(struct audit_context *context)
  485. {
  486. struct audit_aux_data *aux;
  487. while ((aux = context->aux)) {
  488. context->aux = aux->next;
  489. kfree(aux);
  490. }
  491. }
  492. static inline void audit_zero_context(struct audit_context *context,
  493. enum audit_state state)
  494. {
  495. uid_t loginuid = context->loginuid;
  496. memset(context, 0, sizeof(*context));
  497. context->state = state;
  498. context->loginuid = loginuid;
  499. }
  500. static inline struct audit_context *audit_alloc_context(enum audit_state state)
  501. {
  502. struct audit_context *context;
  503. if (!(context = kmalloc(sizeof(*context), GFP_KERNEL)))
  504. return NULL;
  505. audit_zero_context(context, state);
  506. return context;
  507. }
  508. /* Filter on the task information and allocate a per-task audit context
  509. * if necessary. Doing so turns on system call auditing for the
  510. * specified task. This is called from copy_process, so no lock is
  511. * needed. */
  512. int audit_alloc(struct task_struct *tsk)
  513. {
  514. struct audit_context *context;
  515. enum audit_state state;
  516. if (likely(!audit_enabled))
  517. return 0; /* Return if not auditing. */
  518. state = audit_filter_task(tsk);
  519. if (likely(state == AUDIT_DISABLED))
  520. return 0;
  521. if (!(context = audit_alloc_context(state))) {
  522. audit_log_lost("out of memory in audit_alloc");
  523. return -ENOMEM;
  524. }
  525. /* Preserve login uid */
  526. context->loginuid = -1;
  527. if (current->audit_context)
  528. context->loginuid = current->audit_context->loginuid;
  529. tsk->audit_context = context;
  530. set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
  531. return 0;
  532. }
  533. static inline void audit_free_context(struct audit_context *context)
  534. {
  535. struct audit_context *previous;
  536. int count = 0;
  537. do {
  538. previous = context->previous;
  539. if (previous || (count && count < 10)) {
  540. ++count;
  541. printk(KERN_ERR "audit(:%d): major=%d name_count=%d:"
  542. " freeing multiple contexts (%d)\n",
  543. context->serial, context->major,
  544. context->name_count, count);
  545. }
  546. audit_free_names(context);
  547. audit_free_aux(context);
  548. kfree(context);
  549. context = previous;
  550. } while (context);
  551. if (count >= 10)
  552. printk(KERN_ERR "audit: freed %d contexts\n", count);
  553. }
  554. static void audit_log_task_info(struct audit_buffer *ab)
  555. {
  556. char name[sizeof(current->comm)];
  557. struct mm_struct *mm = current->mm;
  558. struct vm_area_struct *vma;
  559. get_task_comm(name, current);
  560. audit_log_format(ab, " comm=%s", name);
  561. if (!mm)
  562. return;
  563. down_read(&mm->mmap_sem);
  564. vma = mm->mmap;
  565. while (vma) {
  566. if ((vma->vm_flags & VM_EXECUTABLE) &&
  567. vma->vm_file) {
  568. audit_log_d_path(ab, "exe=",
  569. vma->vm_file->f_dentry,
  570. vma->vm_file->f_vfsmnt);
  571. break;
  572. }
  573. vma = vma->vm_next;
  574. }
  575. up_read(&mm->mmap_sem);
  576. }
  577. static void audit_log_exit(struct audit_context *context)
  578. {
  579. int i;
  580. struct audit_buffer *ab;
  581. ab = audit_log_start(context);
  582. if (!ab)
  583. return; /* audit_panic has been called */
  584. audit_log_format(ab, "syscall=%d", context->major);
  585. if (context->personality != PER_LINUX)
  586. audit_log_format(ab, " per=%lx", context->personality);
  587. audit_log_format(ab, " arch=%x", context->arch);
  588. if (context->return_valid)
  589. audit_log_format(ab, " success=%s exit=%ld",
  590. (context->return_valid==AUDITSC_SUCCESS)?"yes":"no",
  591. context->return_code);
  592. audit_log_format(ab,
  593. " a0=%lx a1=%lx a2=%lx a3=%lx items=%d"
  594. " pid=%d loginuid=%d uid=%d gid=%d"
  595. " euid=%d suid=%d fsuid=%d"
  596. " egid=%d sgid=%d fsgid=%d",
  597. context->argv[0],
  598. context->argv[1],
  599. context->argv[2],
  600. context->argv[3],
  601. context->name_count,
  602. context->pid,
  603. context->loginuid,
  604. context->uid,
  605. context->gid,
  606. context->euid, context->suid, context->fsuid,
  607. context->egid, context->sgid, context->fsgid);
  608. audit_log_task_info(ab);
  609. audit_log_end(ab);
  610. while (context->aux) {
  611. struct audit_aux_data *aux;
  612. ab = audit_log_start(context);
  613. if (!ab)
  614. continue; /* audit_panic has been called */
  615. aux = context->aux;
  616. context->aux = aux->next;
  617. audit_log_format(ab, "auxitem=%d", aux->type);
  618. switch (aux->type) {
  619. case AUDIT_AUX_IPCPERM: {
  620. struct audit_aux_data_ipcctl *axi = (void *)aux;
  621. audit_log_format(ab,
  622. " qbytes=%lx uid=%d gid=%d mode=%x",
  623. axi->qbytes, axi->uid, axi->gid, axi->mode);
  624. }
  625. }
  626. audit_log_end(ab);
  627. kfree(aux);
  628. }
  629. for (i = 0; i < context->name_count; i++) {
  630. ab = audit_log_start(context);
  631. if (!ab)
  632. continue; /* audit_panic has been called */
  633. audit_log_format(ab, "item=%d", i);
  634. if (context->names[i].name) {
  635. audit_log_format(ab, " name=");
  636. audit_log_untrustedstring(ab, context->names[i].name);
  637. }
  638. if (context->names[i].ino != (unsigned long)-1)
  639. audit_log_format(ab, " inode=%lu dev=%02x:%02x mode=%#o"
  640. " uid=%d gid=%d rdev=%02x:%02x",
  641. context->names[i].ino,
  642. MAJOR(context->names[i].dev),
  643. MINOR(context->names[i].dev),
  644. context->names[i].mode,
  645. context->names[i].uid,
  646. context->names[i].gid,
  647. MAJOR(context->names[i].rdev),
  648. MINOR(context->names[i].rdev));
  649. audit_log_end(ab);
  650. }
  651. }
  652. /* Free a per-task audit context. Called from copy_process and
  653. * __put_task_struct. */
  654. void audit_free(struct task_struct *tsk)
  655. {
  656. struct audit_context *context;
  657. task_lock(tsk);
  658. context = audit_get_context(tsk, 0, 0);
  659. task_unlock(tsk);
  660. if (likely(!context))
  661. return;
  662. /* Check for system calls that do not go through the exit
  663. * function (e.g., exit_group), then free context block. */
  664. if (context->in_syscall && context->auditable)
  665. audit_log_exit(context);
  666. audit_free_context(context);
  667. }
  668. /* Compute a serial number for the audit record. Audit records are
  669. * written to user-space as soon as they are generated, so a complete
  670. * audit record may be written in several pieces. The timestamp of the
  671. * record and this serial number are used by the user-space daemon to
  672. * determine which pieces belong to the same audit record. The
  673. * (timestamp,serial) tuple is unique for each syscall and is live from
  674. * syscall entry to syscall exit.
  675. *
  676. * Atomic values are only guaranteed to be 24-bit, so we count down.
  677. *
  678. * NOTE: Another possibility is to store the formatted records off the
  679. * audit context (for those records that have a context), and emit them
  680. * all at syscall exit. However, this could delay the reporting of
  681. * significant errors until syscall exit (or never, if the system
  682. * halts). */
  683. static inline unsigned int audit_serial(void)
  684. {
  685. static atomic_t serial = ATOMIC_INIT(0xffffff);
  686. unsigned int a, b;
  687. do {
  688. a = atomic_read(&serial);
  689. if (atomic_dec_and_test(&serial))
  690. atomic_set(&serial, 0xffffff);
  691. b = atomic_read(&serial);
  692. } while (b != a - 1);
  693. return 0xffffff - b;
  694. }
  695. /* Fill in audit context at syscall entry. This only happens if the
  696. * audit context was created when the task was created and the state or
  697. * filters demand the audit context be built. If the state from the
  698. * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT,
  699. * then the record will be written at syscall exit time (otherwise, it
  700. * will only be written if another part of the kernel requests that it
  701. * be written). */
  702. void audit_syscall_entry(struct task_struct *tsk, int arch, int major,
  703. unsigned long a1, unsigned long a2,
  704. unsigned long a3, unsigned long a4)
  705. {
  706. struct audit_context *context = tsk->audit_context;
  707. enum audit_state state;
  708. BUG_ON(!context);
  709. /* This happens only on certain architectures that make system
  710. * calls in kernel_thread via the entry.S interface, instead of
  711. * with direct calls. (If you are porting to a new
  712. * architecture, hitting this condition can indicate that you
  713. * got the _exit/_leave calls backward in entry.S.)
  714. *
  715. * i386 no
  716. * x86_64 no
  717. * ppc64 yes (see arch/ppc64/kernel/misc.S)
  718. *
  719. * This also happens with vm86 emulation in a non-nested manner
  720. * (entries without exits), so this case must be caught.
  721. */
  722. if (context->in_syscall) {
  723. struct audit_context *newctx;
  724. #if defined(__NR_vm86) && defined(__NR_vm86old)
  725. /* vm86 mode should only be entered once */
  726. if (major == __NR_vm86 || major == __NR_vm86old)
  727. return;
  728. #endif
  729. #if AUDIT_DEBUG
  730. printk(KERN_ERR
  731. "audit(:%d) pid=%d in syscall=%d;"
  732. " entering syscall=%d\n",
  733. context->serial, tsk->pid, context->major, major);
  734. #endif
  735. newctx = audit_alloc_context(context->state);
  736. if (newctx) {
  737. newctx->previous = context;
  738. context = newctx;
  739. tsk->audit_context = newctx;
  740. } else {
  741. /* If we can't alloc a new context, the best we
  742. * can do is to leak memory (any pending putname
  743. * will be lost). The only other alternative is
  744. * to abandon auditing. */
  745. audit_zero_context(context, context->state);
  746. }
  747. }
  748. BUG_ON(context->in_syscall || context->name_count);
  749. if (!audit_enabled)
  750. return;
  751. context->arch = arch;
  752. context->major = major;
  753. context->argv[0] = a1;
  754. context->argv[1] = a2;
  755. context->argv[2] = a3;
  756. context->argv[3] = a4;
  757. state = context->state;
  758. if (state == AUDIT_SETUP_CONTEXT || state == AUDIT_BUILD_CONTEXT)
  759. state = audit_filter_syscall(tsk, context, &audit_entlist);
  760. if (likely(state == AUDIT_DISABLED))
  761. return;
  762. context->serial = audit_serial();
  763. context->ctime = CURRENT_TIME;
  764. context->in_syscall = 1;
  765. context->auditable = !!(state == AUDIT_RECORD_CONTEXT);
  766. }
  767. /* Tear down after system call. If the audit context has been marked as
  768. * auditable (either because of the AUDIT_RECORD_CONTEXT state from
  769. * filtering, or because some other part of the kernel write an audit
  770. * message), then write out the syscall information. In call cases,
  771. * free the names stored from getname(). */
  772. void audit_syscall_exit(struct task_struct *tsk, int valid, long return_code)
  773. {
  774. struct audit_context *context;
  775. get_task_struct(tsk);
  776. task_lock(tsk);
  777. context = audit_get_context(tsk, valid, return_code);
  778. task_unlock(tsk);
  779. /* Not having a context here is ok, since the parent may have
  780. * called __put_task_struct. */
  781. if (likely(!context))
  782. return;
  783. if (context->in_syscall && context->auditable)
  784. audit_log_exit(context);
  785. context->in_syscall = 0;
  786. context->auditable = 0;
  787. if (context->previous) {
  788. struct audit_context *new_context = context->previous;
  789. context->previous = NULL;
  790. audit_free_context(context);
  791. tsk->audit_context = new_context;
  792. } else {
  793. audit_free_names(context);
  794. audit_free_aux(context);
  795. audit_zero_context(context, context->state);
  796. tsk->audit_context = context;
  797. }
  798. put_task_struct(tsk);
  799. }
  800. /* Add a name to the list. Called from fs/namei.c:getname(). */
  801. void audit_getname(const char *name)
  802. {
  803. struct audit_context *context = current->audit_context;
  804. if (!context || IS_ERR(name) || !name)
  805. return;
  806. if (!context->in_syscall) {
  807. #if AUDIT_DEBUG == 2
  808. printk(KERN_ERR "%s:%d(:%d): ignoring getname(%p)\n",
  809. __FILE__, __LINE__, context->serial, name);
  810. dump_stack();
  811. #endif
  812. return;
  813. }
  814. BUG_ON(context->name_count >= AUDIT_NAMES);
  815. context->names[context->name_count].name = name;
  816. context->names[context->name_count].ino = (unsigned long)-1;
  817. ++context->name_count;
  818. }
  819. /* Intercept a putname request. Called from
  820. * include/linux/fs.h:putname(). If we have stored the name from
  821. * getname in the audit context, then we delay the putname until syscall
  822. * exit. */
  823. void audit_putname(const char *name)
  824. {
  825. struct audit_context *context = current->audit_context;
  826. BUG_ON(!context);
  827. if (!context->in_syscall) {
  828. #if AUDIT_DEBUG == 2
  829. printk(KERN_ERR "%s:%d(:%d): __putname(%p)\n",
  830. __FILE__, __LINE__, context->serial, name);
  831. if (context->name_count) {
  832. int i;
  833. for (i = 0; i < context->name_count; i++)
  834. printk(KERN_ERR "name[%d] = %p = %s\n", i,
  835. context->names[i].name,
  836. context->names[i].name);
  837. }
  838. #endif
  839. __putname(name);
  840. }
  841. #if AUDIT_DEBUG
  842. else {
  843. ++context->put_count;
  844. if (context->put_count > context->name_count) {
  845. printk(KERN_ERR "%s:%d(:%d): major=%d"
  846. " in_syscall=%d putname(%p) name_count=%d"
  847. " put_count=%d\n",
  848. __FILE__, __LINE__,
  849. context->serial, context->major,
  850. context->in_syscall, name, context->name_count,
  851. context->put_count);
  852. dump_stack();
  853. }
  854. }
  855. #endif
  856. }
  857. /* Store the inode and device from a lookup. Called from
  858. * fs/namei.c:path_lookup(). */
  859. void audit_inode(const char *name, const struct inode *inode)
  860. {
  861. int idx;
  862. struct audit_context *context = current->audit_context;
  863. if (!context->in_syscall)
  864. return;
  865. if (context->name_count
  866. && context->names[context->name_count-1].name
  867. && context->names[context->name_count-1].name == name)
  868. idx = context->name_count - 1;
  869. else if (context->name_count > 1
  870. && context->names[context->name_count-2].name
  871. && context->names[context->name_count-2].name == name)
  872. idx = context->name_count - 2;
  873. else {
  874. /* FIXME: how much do we care about inodes that have no
  875. * associated name? */
  876. if (context->name_count >= AUDIT_NAMES - AUDIT_NAMES_RESERVED)
  877. return;
  878. idx = context->name_count++;
  879. context->names[idx].name = NULL;
  880. #if AUDIT_DEBUG
  881. ++context->ino_count;
  882. #endif
  883. }
  884. context->names[idx].ino = inode->i_ino;
  885. context->names[idx].dev = inode->i_sb->s_dev;
  886. context->names[idx].mode = inode->i_mode;
  887. context->names[idx].uid = inode->i_uid;
  888. context->names[idx].gid = inode->i_gid;
  889. context->names[idx].rdev = inode->i_rdev;
  890. }
  891. int audit_get_stamp(struct audit_context *ctx,
  892. struct timespec *t, unsigned int *serial)
  893. {
  894. if (ctx) {
  895. t->tv_sec = ctx->ctime.tv_sec;
  896. t->tv_nsec = ctx->ctime.tv_nsec;
  897. *serial = ctx->serial;
  898. ctx->auditable = 1;
  899. return 1;
  900. }
  901. return 0;
  902. }
  903. extern int audit_set_type(struct audit_buffer *ab, int type);
  904. int audit_set_loginuid(struct task_struct *task, uid_t loginuid)
  905. {
  906. if (task->audit_context) {
  907. struct audit_buffer *ab;
  908. ab = audit_log_start(NULL);
  909. if (ab) {
  910. audit_log_format(ab, "login pid=%d uid=%u "
  911. "old loginuid=%u new loginuid=%u",
  912. task->pid, task->uid,
  913. task->audit_context->loginuid, loginuid);
  914. audit_set_type(ab, AUDIT_LOGIN);
  915. audit_log_end(ab);
  916. }
  917. task->audit_context->loginuid = loginuid;
  918. }
  919. return 0;
  920. }
  921. uid_t audit_get_loginuid(struct audit_context *ctx)
  922. {
  923. return ctx ? ctx->loginuid : -1;
  924. }
  925. int audit_ipc_perms(unsigned long qbytes, uid_t uid, gid_t gid, mode_t mode)
  926. {
  927. struct audit_aux_data_ipcctl *ax;
  928. struct audit_context *context = current->audit_context;
  929. if (likely(!context))
  930. return 0;
  931. ax = kmalloc(sizeof(*ax), GFP_KERNEL);
  932. if (!ax)
  933. return -ENOMEM;
  934. ax->qbytes = qbytes;
  935. ax->uid = uid;
  936. ax->gid = gid;
  937. ax->mode = mode;
  938. ax->d.type = AUDIT_AUX_IPCPERM;
  939. ax->d.next = context->aux;
  940. context->aux = (void *)ax;
  941. return 0;
  942. }
  943. void audit_signal_info(int sig, struct task_struct *t)
  944. {
  945. extern pid_t audit_sig_pid;
  946. extern uid_t audit_sig_uid;
  947. extern int audit_pid;
  948. if (unlikely(audit_pid && t->pid == audit_pid)) {
  949. if (sig == SIGTERM || sig == SIGHUP) {
  950. struct audit_context *ctx = current->audit_context;
  951. audit_sig_pid = current->pid;
  952. if (ctx)
  953. audit_sig_uid = ctx->loginuid;
  954. else
  955. audit_sig_uid = current->uid;
  956. }
  957. }
  958. }