auditsc.c 30 KB

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