auditsc.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054
  1. /* auditsc.c -- System-call auditing support -*- linux-c -*-
  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. #ifdef CONFIG_NET
  201. /* Copy rule from user-space to kernel-space. Called during
  202. * AUDIT_ADD. */
  203. static int audit_copy_rule(struct audit_rule *d, struct audit_rule *s)
  204. {
  205. int i;
  206. if (s->action != AUDIT_NEVER
  207. && s->action != AUDIT_POSSIBLE
  208. && s->action != AUDIT_ALWAYS)
  209. return -1;
  210. if (s->field_count < 0 || s->field_count > AUDIT_MAX_FIELDS)
  211. return -1;
  212. d->flags = s->flags;
  213. d->action = s->action;
  214. d->field_count = s->field_count;
  215. for (i = 0; i < d->field_count; i++) {
  216. d->fields[i] = s->fields[i];
  217. d->values[i] = s->values[i];
  218. }
  219. for (i = 0; i < AUDIT_BITMASK_SIZE; i++) d->mask[i] = s->mask[i];
  220. return 0;
  221. }
  222. int audit_receive_filter(int type, int pid, int uid, int seq, void *data)
  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. break;
  257. case AUDIT_DEL:
  258. flags =((struct audit_rule *)data)->flags;
  259. if (!err && (flags & AUDIT_PER_TASK))
  260. err = audit_del_rule(data, &audit_tsklist);
  261. if (!err && (flags & AUDIT_AT_ENTRY))
  262. err = audit_del_rule(data, &audit_entlist);
  263. if (!err && (flags & AUDIT_AT_EXIT))
  264. err = audit_del_rule(data, &audit_extlist);
  265. break;
  266. default:
  267. return -EINVAL;
  268. }
  269. return err;
  270. }
  271. #endif
  272. /* Compare a task_struct with an audit_rule. Return 1 on match, 0
  273. * otherwise. */
  274. static int audit_filter_rules(struct task_struct *tsk,
  275. struct audit_rule *rule,
  276. struct audit_context *ctx,
  277. enum audit_state *state)
  278. {
  279. int i, j;
  280. for (i = 0; i < rule->field_count; i++) {
  281. u32 field = rule->fields[i] & ~AUDIT_NEGATE;
  282. u32 value = rule->values[i];
  283. int result = 0;
  284. switch (field) {
  285. case AUDIT_PID:
  286. result = (tsk->pid == value);
  287. break;
  288. case AUDIT_UID:
  289. result = (tsk->uid == value);
  290. break;
  291. case AUDIT_EUID:
  292. result = (tsk->euid == value);
  293. break;
  294. case AUDIT_SUID:
  295. result = (tsk->suid == value);
  296. break;
  297. case AUDIT_FSUID:
  298. result = (tsk->fsuid == value);
  299. break;
  300. case AUDIT_GID:
  301. result = (tsk->gid == value);
  302. break;
  303. case AUDIT_EGID:
  304. result = (tsk->egid == value);
  305. break;
  306. case AUDIT_SGID:
  307. result = (tsk->sgid == value);
  308. break;
  309. case AUDIT_FSGID:
  310. result = (tsk->fsgid == value);
  311. break;
  312. case AUDIT_PERS:
  313. result = (tsk->personality == value);
  314. break;
  315. case AUDIT_ARCH:
  316. if (ctx)
  317. result = (ctx->arch == value);
  318. break;
  319. case AUDIT_EXIT:
  320. if (ctx && ctx->return_valid)
  321. result = (ctx->return_code == value);
  322. break;
  323. case AUDIT_SUCCESS:
  324. if (ctx && ctx->return_valid)
  325. result = (ctx->return_valid == AUDITSC_SUCCESS);
  326. break;
  327. case AUDIT_DEVMAJOR:
  328. if (ctx) {
  329. for (j = 0; j < ctx->name_count; j++) {
  330. if (MAJOR(ctx->names[j].dev)==value) {
  331. ++result;
  332. break;
  333. }
  334. }
  335. }
  336. break;
  337. case AUDIT_DEVMINOR:
  338. if (ctx) {
  339. for (j = 0; j < ctx->name_count; j++) {
  340. if (MINOR(ctx->names[j].dev)==value) {
  341. ++result;
  342. break;
  343. }
  344. }
  345. }
  346. break;
  347. case AUDIT_INODE:
  348. if (ctx) {
  349. for (j = 0; j < ctx->name_count; j++) {
  350. if (ctx->names[j].ino == value) {
  351. ++result;
  352. break;
  353. }
  354. }
  355. }
  356. break;
  357. case AUDIT_LOGINUID:
  358. result = 0;
  359. if (ctx)
  360. result = (ctx->loginuid == value);
  361. break;
  362. case AUDIT_ARG0:
  363. case AUDIT_ARG1:
  364. case AUDIT_ARG2:
  365. case AUDIT_ARG3:
  366. if (ctx)
  367. result = (ctx->argv[field-AUDIT_ARG0]==value);
  368. break;
  369. }
  370. if (rule->fields[i] & AUDIT_NEGATE)
  371. result = !result;
  372. if (!result)
  373. return 0;
  374. }
  375. switch (rule->action) {
  376. case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
  377. case AUDIT_POSSIBLE: *state = AUDIT_BUILD_CONTEXT; break;
  378. case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
  379. }
  380. return 1;
  381. }
  382. /* At process creation time, we can determine if system-call auditing is
  383. * completely disabled for this task. Since we only have the task
  384. * structure at this point, we can only check uid and gid.
  385. */
  386. static enum audit_state audit_filter_task(struct task_struct *tsk)
  387. {
  388. struct audit_entry *e;
  389. enum audit_state state;
  390. rcu_read_lock();
  391. list_for_each_entry_rcu(e, &audit_tsklist, list) {
  392. if (audit_filter_rules(tsk, &e->rule, NULL, &state)) {
  393. rcu_read_unlock();
  394. return state;
  395. }
  396. }
  397. rcu_read_unlock();
  398. return AUDIT_BUILD_CONTEXT;
  399. }
  400. /* At syscall entry and exit time, this filter is called if the
  401. * audit_state is not low enough that auditing cannot take place, but is
  402. * also not high enough that we already know we have to write and audit
  403. * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT).
  404. */
  405. static enum audit_state audit_filter_syscall(struct task_struct *tsk,
  406. struct audit_context *ctx,
  407. struct list_head *list)
  408. {
  409. struct audit_entry *e;
  410. enum audit_state state;
  411. int word = AUDIT_WORD(ctx->major);
  412. int bit = AUDIT_BIT(ctx->major);
  413. rcu_read_lock();
  414. list_for_each_entry_rcu(e, list, list) {
  415. if ((e->rule.mask[word] & bit) == bit
  416. && audit_filter_rules(tsk, &e->rule, ctx, &state)) {
  417. rcu_read_unlock();
  418. return state;
  419. }
  420. }
  421. rcu_read_unlock();
  422. return AUDIT_BUILD_CONTEXT;
  423. }
  424. /* This should be called with task_lock() held. */
  425. static inline struct audit_context *audit_get_context(struct task_struct *tsk,
  426. int return_valid,
  427. int return_code)
  428. {
  429. struct audit_context *context = tsk->audit_context;
  430. if (likely(!context))
  431. return NULL;
  432. context->return_valid = return_valid;
  433. context->return_code = return_code;
  434. if (context->in_syscall && !context->auditable) {
  435. enum audit_state state;
  436. state = audit_filter_syscall(tsk, context, &audit_extlist);
  437. if (state == AUDIT_RECORD_CONTEXT)
  438. context->auditable = 1;
  439. }
  440. context->pid = tsk->pid;
  441. context->uid = tsk->uid;
  442. context->gid = tsk->gid;
  443. context->euid = tsk->euid;
  444. context->suid = tsk->suid;
  445. context->fsuid = tsk->fsuid;
  446. context->egid = tsk->egid;
  447. context->sgid = tsk->sgid;
  448. context->fsgid = tsk->fsgid;
  449. context->personality = tsk->personality;
  450. tsk->audit_context = NULL;
  451. return context;
  452. }
  453. static inline void audit_free_names(struct audit_context *context)
  454. {
  455. int i;
  456. #if AUDIT_DEBUG == 2
  457. if (context->auditable
  458. ||context->put_count + context->ino_count != context->name_count) {
  459. printk(KERN_ERR "audit.c:%d(:%d): major=%d in_syscall=%d"
  460. " name_count=%d put_count=%d"
  461. " ino_count=%d [NOT freeing]\n",
  462. __LINE__,
  463. context->serial, context->major, context->in_syscall,
  464. context->name_count, context->put_count,
  465. context->ino_count);
  466. for (i = 0; i < context->name_count; i++)
  467. printk(KERN_ERR "names[%d] = %p = %s\n", i,
  468. context->names[i].name,
  469. context->names[i].name);
  470. dump_stack();
  471. return;
  472. }
  473. #endif
  474. #if AUDIT_DEBUG
  475. context->put_count = 0;
  476. context->ino_count = 0;
  477. #endif
  478. for (i = 0; i < context->name_count; i++)
  479. if (context->names[i].name)
  480. __putname(context->names[i].name);
  481. context->name_count = 0;
  482. }
  483. static inline void audit_free_aux(struct audit_context *context)
  484. {
  485. struct audit_aux_data *aux;
  486. while ((aux = context->aux)) {
  487. context->aux = aux->next;
  488. kfree(aux);
  489. }
  490. }
  491. static inline void audit_zero_context(struct audit_context *context,
  492. enum audit_state state)
  493. {
  494. uid_t loginuid = context->loginuid;
  495. memset(context, 0, sizeof(*context));
  496. context->state = state;
  497. context->loginuid = loginuid;
  498. }
  499. static inline struct audit_context *audit_alloc_context(enum audit_state state)
  500. {
  501. struct audit_context *context;
  502. if (!(context = kmalloc(sizeof(*context), GFP_KERNEL)))
  503. return NULL;
  504. audit_zero_context(context, state);
  505. return context;
  506. }
  507. /* Filter on the task information and allocate a per-task audit context
  508. * if necessary. Doing so turns on system call auditing for the
  509. * specified task. This is called from copy_process, so no lock is
  510. * needed. */
  511. int audit_alloc(struct task_struct *tsk)
  512. {
  513. struct audit_context *context;
  514. enum audit_state state;
  515. if (likely(!audit_enabled))
  516. return 0; /* Return if not auditing. */
  517. state = audit_filter_task(tsk);
  518. if (likely(state == AUDIT_DISABLED))
  519. return 0;
  520. if (!(context = audit_alloc_context(state))) {
  521. audit_log_lost("out of memory in audit_alloc");
  522. return -ENOMEM;
  523. }
  524. /* Preserve login uid */
  525. context->loginuid = -1;
  526. if (current->audit_context)
  527. context->loginuid = current->audit_context->loginuid;
  528. tsk->audit_context = context;
  529. set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
  530. return 0;
  531. }
  532. static inline void audit_free_context(struct audit_context *context)
  533. {
  534. struct audit_context *previous;
  535. int count = 0;
  536. do {
  537. previous = context->previous;
  538. if (previous || (count && count < 10)) {
  539. ++count;
  540. printk(KERN_ERR "audit(:%d): major=%d name_count=%d:"
  541. " freeing multiple contexts (%d)\n",
  542. context->serial, context->major,
  543. context->name_count, count);
  544. }
  545. audit_free_names(context);
  546. audit_free_aux(context);
  547. kfree(context);
  548. context = previous;
  549. } while (context);
  550. if (count >= 10)
  551. printk(KERN_ERR "audit: freed %d contexts\n", count);
  552. }
  553. static void audit_log_task_info(struct audit_buffer *ab)
  554. {
  555. char name[sizeof(current->comm)];
  556. struct mm_struct *mm = current->mm;
  557. struct vm_area_struct *vma;
  558. get_task_comm(name, current);
  559. audit_log_format(ab, " comm=%s", name);
  560. if (!mm)
  561. return;
  562. down_read(&mm->mmap_sem);
  563. vma = mm->mmap;
  564. while (vma) {
  565. if ((vma->vm_flags & VM_EXECUTABLE) &&
  566. vma->vm_file) {
  567. audit_log_d_path(ab, "exe=",
  568. vma->vm_file->f_dentry,
  569. vma->vm_file->f_vfsmnt);
  570. break;
  571. }
  572. vma = vma->vm_next;
  573. }
  574. up_read(&mm->mmap_sem);
  575. }
  576. static void audit_log_exit(struct audit_context *context)
  577. {
  578. int i;
  579. struct audit_buffer *ab;
  580. ab = audit_log_start(context);
  581. if (!ab)
  582. return; /* audit_panic has been called */
  583. audit_log_format(ab, "syscall=%d", context->major);
  584. if (context->personality != PER_LINUX)
  585. audit_log_format(ab, " per=%lx", context->personality);
  586. audit_log_format(ab, " arch=%x", context->arch);
  587. if (context->return_valid)
  588. audit_log_format(ab, " success=%s exit=%ld",
  589. (context->return_valid==AUDITSC_SUCCESS)?"yes":"no",
  590. context->return_code);
  591. audit_log_format(ab,
  592. " a0=%lx a1=%lx a2=%lx a3=%lx items=%d"
  593. " pid=%d loginuid=%d uid=%d gid=%d"
  594. " euid=%d suid=%d fsuid=%d"
  595. " egid=%d sgid=%d fsgid=%d",
  596. context->argv[0],
  597. context->argv[1],
  598. context->argv[2],
  599. context->argv[3],
  600. context->name_count,
  601. context->pid,
  602. context->loginuid,
  603. context->uid,
  604. context->gid,
  605. context->euid, context->suid, context->fsuid,
  606. context->egid, context->sgid, context->fsgid);
  607. audit_log_task_info(ab);
  608. audit_log_end(ab);
  609. while (context->aux) {
  610. struct audit_aux_data *aux;
  611. ab = audit_log_start(context);
  612. if (!ab)
  613. continue; /* audit_panic has been called */
  614. aux = context->aux;
  615. context->aux = aux->next;
  616. audit_log_format(ab, "auxitem=%d", aux->type);
  617. switch (aux->type) {
  618. case AUDIT_AUX_IPCPERM: {
  619. struct audit_aux_data_ipcctl *axi = (void *)aux;
  620. audit_log_format(ab,
  621. " qbytes=%lx uid=%d gid=%d mode=%x",
  622. axi->qbytes, axi->uid, axi->gid, axi->mode);
  623. }
  624. }
  625. audit_log_end(ab);
  626. kfree(aux);
  627. }
  628. for (i = 0; i < context->name_count; i++) {
  629. ab = audit_log_start(context);
  630. if (!ab)
  631. continue; /* audit_panic has been called */
  632. audit_log_format(ab, "item=%d", i);
  633. if (context->names[i].name) {
  634. audit_log_format(ab, " name=");
  635. audit_log_untrustedstring(ab, context->names[i].name);
  636. }
  637. if (context->names[i].ino != (unsigned long)-1)
  638. audit_log_format(ab, " inode=%lu dev=%02x:%02x mode=%#o"
  639. " uid=%d gid=%d rdev=%02x:%02x",
  640. context->names[i].ino,
  641. MAJOR(context->names[i].dev),
  642. MINOR(context->names[i].dev),
  643. context->names[i].mode,
  644. context->names[i].uid,
  645. context->names[i].gid,
  646. MAJOR(context->names[i].rdev),
  647. MINOR(context->names[i].rdev));
  648. audit_log_end(ab);
  649. }
  650. }
  651. /* Free a per-task audit context. Called from copy_process and
  652. * __put_task_struct. */
  653. void audit_free(struct task_struct *tsk)
  654. {
  655. struct audit_context *context;
  656. task_lock(tsk);
  657. context = audit_get_context(tsk, 0, 0);
  658. task_unlock(tsk);
  659. if (likely(!context))
  660. return;
  661. /* Check for system calls that do not go through the exit
  662. * function (e.g., exit_group), then free context block. */
  663. if (context->in_syscall && context->auditable)
  664. audit_log_exit(context);
  665. audit_free_context(context);
  666. }
  667. /* Compute a serial number for the audit record. Audit records are
  668. * written to user-space as soon as they are generated, so a complete
  669. * audit record may be written in several pieces. The timestamp of the
  670. * record and this serial number are used by the user-space daemon to
  671. * determine which pieces belong to the same audit record. The
  672. * (timestamp,serial) tuple is unique for each syscall and is live from
  673. * syscall entry to syscall exit.
  674. *
  675. * Atomic values are only guaranteed to be 24-bit, so we count down.
  676. *
  677. * NOTE: Another possibility is to store the formatted records off the
  678. * audit context (for those records that have a context), and emit them
  679. * all at syscall exit. However, this could delay the reporting of
  680. * significant errors until syscall exit (or never, if the system
  681. * halts). */
  682. static inline unsigned int audit_serial(void)
  683. {
  684. static atomic_t serial = ATOMIC_INIT(0xffffff);
  685. unsigned int a, b;
  686. do {
  687. a = atomic_read(&serial);
  688. if (atomic_dec_and_test(&serial))
  689. atomic_set(&serial, 0xffffff);
  690. b = atomic_read(&serial);
  691. } while (b != a - 1);
  692. return 0xffffff - b;
  693. }
  694. /* Fill in audit context at syscall entry. This only happens if the
  695. * audit context was created when the task was created and the state or
  696. * filters demand the audit context be built. If the state from the
  697. * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT,
  698. * then the record will be written at syscall exit time (otherwise, it
  699. * will only be written if another part of the kernel requests that it
  700. * be written). */
  701. void audit_syscall_entry(struct task_struct *tsk, int arch, int major,
  702. unsigned long a1, unsigned long a2,
  703. unsigned long a3, unsigned long a4)
  704. {
  705. struct audit_context *context = tsk->audit_context;
  706. enum audit_state state;
  707. BUG_ON(!context);
  708. /* This happens only on certain architectures that make system
  709. * calls in kernel_thread via the entry.S interface, instead of
  710. * with direct calls. (If you are porting to a new
  711. * architecture, hitting this condition can indicate that you
  712. * got the _exit/_leave calls backward in entry.S.)
  713. *
  714. * i386 no
  715. * x86_64 no
  716. * ppc64 yes (see arch/ppc64/kernel/misc.S)
  717. *
  718. * This also happens with vm86 emulation in a non-nested manner
  719. * (entries without exits), so this case must be caught.
  720. */
  721. if (context->in_syscall) {
  722. struct audit_context *newctx;
  723. #if defined(__NR_vm86) && defined(__NR_vm86old)
  724. /* vm86 mode should only be entered once */
  725. if (major == __NR_vm86 || major == __NR_vm86old)
  726. return;
  727. #endif
  728. #if AUDIT_DEBUG
  729. printk(KERN_ERR
  730. "audit(:%d) pid=%d in syscall=%d;"
  731. " entering syscall=%d\n",
  732. context->serial, tsk->pid, context->major, major);
  733. #endif
  734. newctx = audit_alloc_context(context->state);
  735. if (newctx) {
  736. newctx->previous = context;
  737. context = newctx;
  738. tsk->audit_context = newctx;
  739. } else {
  740. /* If we can't alloc a new context, the best we
  741. * can do is to leak memory (any pending putname
  742. * will be lost). The only other alternative is
  743. * to abandon auditing. */
  744. audit_zero_context(context, context->state);
  745. }
  746. }
  747. BUG_ON(context->in_syscall || context->name_count);
  748. if (!audit_enabled)
  749. return;
  750. context->arch = arch;
  751. context->major = major;
  752. context->argv[0] = a1;
  753. context->argv[1] = a2;
  754. context->argv[2] = a3;
  755. context->argv[3] = a4;
  756. state = context->state;
  757. if (state == AUDIT_SETUP_CONTEXT || state == AUDIT_BUILD_CONTEXT)
  758. state = audit_filter_syscall(tsk, context, &audit_entlist);
  759. if (likely(state == AUDIT_DISABLED))
  760. return;
  761. context->serial = audit_serial();
  762. context->ctime = CURRENT_TIME;
  763. context->in_syscall = 1;
  764. context->auditable = !!(state == AUDIT_RECORD_CONTEXT);
  765. }
  766. /* Tear down after system call. If the audit context has been marked as
  767. * auditable (either because of the AUDIT_RECORD_CONTEXT state from
  768. * filtering, or because some other part of the kernel write an audit
  769. * message), then write out the syscall information. In call cases,
  770. * free the names stored from getname(). */
  771. void audit_syscall_exit(struct task_struct *tsk, int valid, long return_code)
  772. {
  773. struct audit_context *context;
  774. get_task_struct(tsk);
  775. task_lock(tsk);
  776. context = audit_get_context(tsk, valid, return_code);
  777. task_unlock(tsk);
  778. /* Not having a context here is ok, since the parent may have
  779. * called __put_task_struct. */
  780. if (likely(!context))
  781. return;
  782. if (context->in_syscall && context->auditable)
  783. audit_log_exit(context);
  784. context->in_syscall = 0;
  785. context->auditable = 0;
  786. if (context->previous) {
  787. struct audit_context *new_context = context->previous;
  788. context->previous = NULL;
  789. audit_free_context(context);
  790. tsk->audit_context = new_context;
  791. } else {
  792. audit_free_names(context);
  793. audit_free_aux(context);
  794. audit_zero_context(context, context->state);
  795. tsk->audit_context = context;
  796. }
  797. put_task_struct(tsk);
  798. }
  799. /* Add a name to the list. Called from fs/namei.c:getname(). */
  800. void audit_getname(const char *name)
  801. {
  802. struct audit_context *context = current->audit_context;
  803. if (!context || IS_ERR(name) || !name)
  804. return;
  805. if (!context->in_syscall) {
  806. #if AUDIT_DEBUG == 2
  807. printk(KERN_ERR "%s:%d(:%d): ignoring getname(%p)\n",
  808. __FILE__, __LINE__, context->serial, name);
  809. dump_stack();
  810. #endif
  811. return;
  812. }
  813. BUG_ON(context->name_count >= AUDIT_NAMES);
  814. context->names[context->name_count].name = name;
  815. context->names[context->name_count].ino = (unsigned long)-1;
  816. ++context->name_count;
  817. }
  818. /* Intercept a putname request. Called from
  819. * include/linux/fs.h:putname(). If we have stored the name from
  820. * getname in the audit context, then we delay the putname until syscall
  821. * exit. */
  822. void audit_putname(const char *name)
  823. {
  824. struct audit_context *context = current->audit_context;
  825. BUG_ON(!context);
  826. if (!context->in_syscall) {
  827. #if AUDIT_DEBUG == 2
  828. printk(KERN_ERR "%s:%d(:%d): __putname(%p)\n",
  829. __FILE__, __LINE__, context->serial, name);
  830. if (context->name_count) {
  831. int i;
  832. for (i = 0; i < context->name_count; i++)
  833. printk(KERN_ERR "name[%d] = %p = %s\n", i,
  834. context->names[i].name,
  835. context->names[i].name);
  836. }
  837. #endif
  838. __putname(name);
  839. }
  840. #if AUDIT_DEBUG
  841. else {
  842. ++context->put_count;
  843. if (context->put_count > context->name_count) {
  844. printk(KERN_ERR "%s:%d(:%d): major=%d"
  845. " in_syscall=%d putname(%p) name_count=%d"
  846. " put_count=%d\n",
  847. __FILE__, __LINE__,
  848. context->serial, context->major,
  849. context->in_syscall, name, context->name_count,
  850. context->put_count);
  851. dump_stack();
  852. }
  853. }
  854. #endif
  855. }
  856. /* Store the inode and device from a lookup. Called from
  857. * fs/namei.c:path_lookup(). */
  858. void audit_inode(const char *name, const struct inode *inode)
  859. {
  860. int idx;
  861. struct audit_context *context = current->audit_context;
  862. if (!context->in_syscall)
  863. return;
  864. if (context->name_count
  865. && context->names[context->name_count-1].name
  866. && context->names[context->name_count-1].name == name)
  867. idx = context->name_count - 1;
  868. else if (context->name_count > 1
  869. && context->names[context->name_count-2].name
  870. && context->names[context->name_count-2].name == name)
  871. idx = context->name_count - 2;
  872. else {
  873. /* FIXME: how much do we care about inodes that have no
  874. * associated name? */
  875. if (context->name_count >= AUDIT_NAMES - AUDIT_NAMES_RESERVED)
  876. return;
  877. idx = context->name_count++;
  878. context->names[idx].name = NULL;
  879. #if AUDIT_DEBUG
  880. ++context->ino_count;
  881. #endif
  882. }
  883. context->names[idx].ino = inode->i_ino;
  884. context->names[idx].dev = inode->i_sb->s_dev;
  885. context->names[idx].mode = inode->i_mode;
  886. context->names[idx].uid = inode->i_uid;
  887. context->names[idx].gid = inode->i_gid;
  888. context->names[idx].rdev = inode->i_rdev;
  889. }
  890. void audit_get_stamp(struct audit_context *ctx,
  891. struct timespec *t, int *serial)
  892. {
  893. if (ctx) {
  894. t->tv_sec = ctx->ctime.tv_sec;
  895. t->tv_nsec = ctx->ctime.tv_nsec;
  896. *serial = ctx->serial;
  897. ctx->auditable = 1;
  898. } else {
  899. *t = CURRENT_TIME;
  900. *serial = 0;
  901. }
  902. }
  903. extern int audit_set_type(struct audit_buffer *ab, int type);
  904. int audit_set_loginuid(struct audit_context *ctx, uid_t loginuid)
  905. {
  906. if (ctx) {
  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. ctx->pid, ctx->uid, ctx->loginuid, loginuid);
  913. audit_set_type(ab, AUDIT_LOGIN);
  914. audit_log_end(ab);
  915. }
  916. ctx->loginuid = loginuid;
  917. }
  918. return 0;
  919. }
  920. uid_t audit_get_loginuid(struct audit_context *ctx)
  921. {
  922. return ctx ? ctx->loginuid : -1;
  923. }
  924. int audit_ipc_perms(unsigned long qbytes, uid_t uid, gid_t gid, mode_t mode)
  925. {
  926. struct audit_aux_data_ipcctl *ax;
  927. struct audit_context *context = current->audit_context;
  928. if (likely(!context))
  929. return 0;
  930. ax = kmalloc(sizeof(*ax), GFP_KERNEL);
  931. if (!ax)
  932. return -ENOMEM;
  933. ax->qbytes = qbytes;
  934. ax->uid = uid;
  935. ax->gid = gid;
  936. ax->mode = mode;
  937. ax->d.type = AUDIT_AUX_IPCPERM;
  938. ax->d.next = context->aux;
  939. context->aux = (void *)ax;
  940. return 0;
  941. }