auditsc.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183
  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 dentry * pwd;
  135. struct vfsmount * pwdmnt;
  136. struct audit_context *previous; /* For nested syscalls */
  137. struct audit_aux_data *aux;
  138. /* Save things to print about task_struct */
  139. pid_t pid;
  140. uid_t uid, euid, suid, fsuid;
  141. gid_t gid, egid, sgid, fsgid;
  142. unsigned long personality;
  143. int arch;
  144. #if AUDIT_DEBUG
  145. int put_count;
  146. int ino_count;
  147. #endif
  148. };
  149. /* Public API */
  150. /* There are three lists of rules -- one to search at task creation
  151. * time, one to search at syscall entry time, and another to search at
  152. * syscall exit time. */
  153. static struct list_head audit_filter_list[AUDIT_NR_FILTERS] = {
  154. LIST_HEAD_INIT(audit_filter_list[0]),
  155. LIST_HEAD_INIT(audit_filter_list[1]),
  156. LIST_HEAD_INIT(audit_filter_list[2]),
  157. LIST_HEAD_INIT(audit_filter_list[3]),
  158. LIST_HEAD_INIT(audit_filter_list[4]),
  159. #if AUDIT_NR_FILTERS != 5
  160. #error Fix audit_filter_list initialiser
  161. #endif
  162. };
  163. struct audit_entry {
  164. struct list_head list;
  165. struct rcu_head rcu;
  166. struct audit_rule rule;
  167. };
  168. extern int audit_pid;
  169. /* Check to see if two rules are identical. It is called from
  170. * audit_del_rule during AUDIT_DEL. */
  171. static int audit_compare_rule(struct audit_rule *a, struct audit_rule *b)
  172. {
  173. int i;
  174. if (a->flags != b->flags)
  175. return 1;
  176. if (a->action != b->action)
  177. return 1;
  178. if (a->field_count != b->field_count)
  179. return 1;
  180. for (i = 0; i < a->field_count; i++) {
  181. if (a->fields[i] != b->fields[i]
  182. || a->values[i] != b->values[i])
  183. return 1;
  184. }
  185. for (i = 0; i < AUDIT_BITMASK_SIZE; i++)
  186. if (a->mask[i] != b->mask[i])
  187. return 1;
  188. return 0;
  189. }
  190. /* Note that audit_add_rule and audit_del_rule are called via
  191. * audit_receive() in audit.c, and are protected by
  192. * audit_netlink_sem. */
  193. static inline void audit_add_rule(struct audit_entry *entry,
  194. struct list_head *list)
  195. {
  196. if (entry->rule.flags & AUDIT_FILTER_PREPEND) {
  197. entry->rule.flags &= ~AUDIT_FILTER_PREPEND;
  198. list_add_rcu(&entry->list, list);
  199. } else {
  200. list_add_tail_rcu(&entry->list, list);
  201. }
  202. }
  203. static void audit_free_rule(struct rcu_head *head)
  204. {
  205. struct audit_entry *e = container_of(head, struct audit_entry, rcu);
  206. kfree(e);
  207. }
  208. /* Note that audit_add_rule and audit_del_rule are called via
  209. * audit_receive() in audit.c, and are protected by
  210. * audit_netlink_sem. */
  211. static inline int audit_del_rule(struct audit_rule *rule,
  212. struct list_head *list)
  213. {
  214. struct audit_entry *e;
  215. /* Do not use the _rcu iterator here, since this is the only
  216. * deletion routine. */
  217. list_for_each_entry(e, list, list) {
  218. if (!audit_compare_rule(rule, &e->rule)) {
  219. list_del_rcu(&e->list);
  220. call_rcu(&e->rcu, audit_free_rule);
  221. return 0;
  222. }
  223. }
  224. return -ENOENT; /* No matching rule */
  225. }
  226. /* Copy rule from user-space to kernel-space. Called during
  227. * AUDIT_ADD. */
  228. static int audit_copy_rule(struct audit_rule *d, struct audit_rule *s)
  229. {
  230. int i;
  231. if (s->action != AUDIT_NEVER
  232. && s->action != AUDIT_POSSIBLE
  233. && s->action != AUDIT_ALWAYS)
  234. return -1;
  235. if (s->field_count < 0 || s->field_count > AUDIT_MAX_FIELDS)
  236. return -1;
  237. if ((s->flags & ~AUDIT_FILTER_PREPEND) >= AUDIT_NR_FILTERS)
  238. return -1;
  239. d->flags = s->flags;
  240. d->action = s->action;
  241. d->field_count = s->field_count;
  242. for (i = 0; i < d->field_count; i++) {
  243. d->fields[i] = s->fields[i];
  244. d->values[i] = s->values[i];
  245. }
  246. for (i = 0; i < AUDIT_BITMASK_SIZE; i++) d->mask[i] = s->mask[i];
  247. return 0;
  248. }
  249. int audit_receive_filter(int type, int pid, int uid, int seq, void *data,
  250. uid_t loginuid)
  251. {
  252. struct audit_entry *entry;
  253. int err = 0;
  254. int i;
  255. unsigned listnr;
  256. switch (type) {
  257. case AUDIT_LIST:
  258. /* The *_rcu iterators not needed here because we are
  259. always called with audit_netlink_sem held. */
  260. for (i=0; i<AUDIT_NR_FILTERS; i++) {
  261. list_for_each_entry(entry, &audit_filter_list[i], list)
  262. audit_send_reply(pid, seq, AUDIT_LIST, 0, 1,
  263. &entry->rule, sizeof(entry->rule));
  264. }
  265. audit_send_reply(pid, seq, AUDIT_LIST, 1, 1, NULL, 0);
  266. break;
  267. case AUDIT_ADD:
  268. if (!(entry = kmalloc(sizeof(*entry), GFP_KERNEL)))
  269. return -ENOMEM;
  270. if (audit_copy_rule(&entry->rule, data)) {
  271. kfree(entry);
  272. return -EINVAL;
  273. }
  274. listnr = entry->rule.flags & ~AUDIT_FILTER_PREPEND;
  275. audit_add_rule(entry, &audit_filter_list[listnr]);
  276. audit_log(NULL, AUDIT_CONFIG_CHANGE,
  277. "auid=%u added an audit rule\n", loginuid);
  278. break;
  279. case AUDIT_DEL:
  280. listnr =((struct audit_rule *)data)->flags & ~AUDIT_FILTER_PREPEND;
  281. if (listnr >= AUDIT_NR_FILTERS)
  282. return -EINVAL;
  283. err = audit_del_rule(data, &audit_filter_list[listnr]);
  284. if (!err)
  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_filter_list[AUDIT_FILTER_TASK], 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. int audit_filter_user(struct task_struct *tsk, int type)
  446. {
  447. struct audit_entry *e;
  448. enum audit_state state;
  449. rcu_read_lock();
  450. list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_USER], list) {
  451. if (audit_filter_rules(tsk, &e->rule, NULL, &state)) {
  452. rcu_read_unlock();
  453. return state != AUDIT_DISABLED;
  454. }
  455. }
  456. rcu_read_unlock();
  457. return 1; /* Audit by default */
  458. }
  459. /* This should be called with task_lock() held. */
  460. static inline struct audit_context *audit_get_context(struct task_struct *tsk,
  461. int return_valid,
  462. int return_code)
  463. {
  464. struct audit_context *context = tsk->audit_context;
  465. if (likely(!context))
  466. return NULL;
  467. context->return_valid = return_valid;
  468. context->return_code = return_code;
  469. if (context->in_syscall && !context->auditable) {
  470. enum audit_state state;
  471. state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_EXIT]);
  472. if (state == AUDIT_RECORD_CONTEXT)
  473. context->auditable = 1;
  474. }
  475. context->pid = tsk->pid;
  476. context->uid = tsk->uid;
  477. context->gid = tsk->gid;
  478. context->euid = tsk->euid;
  479. context->suid = tsk->suid;
  480. context->fsuid = tsk->fsuid;
  481. context->egid = tsk->egid;
  482. context->sgid = tsk->sgid;
  483. context->fsgid = tsk->fsgid;
  484. context->personality = tsk->personality;
  485. tsk->audit_context = NULL;
  486. return context;
  487. }
  488. static inline void audit_free_names(struct audit_context *context)
  489. {
  490. int i;
  491. #if AUDIT_DEBUG == 2
  492. if (context->auditable
  493. ||context->put_count + context->ino_count != context->name_count) {
  494. printk(KERN_ERR "audit.c:%d(:%d): major=%d in_syscall=%d"
  495. " name_count=%d put_count=%d"
  496. " ino_count=%d [NOT freeing]\n",
  497. __LINE__,
  498. context->serial, context->major, context->in_syscall,
  499. context->name_count, context->put_count,
  500. context->ino_count);
  501. for (i = 0; i < context->name_count; i++)
  502. printk(KERN_ERR "names[%d] = %p = %s\n", i,
  503. context->names[i].name,
  504. context->names[i].name);
  505. dump_stack();
  506. return;
  507. }
  508. #endif
  509. #if AUDIT_DEBUG
  510. context->put_count = 0;
  511. context->ino_count = 0;
  512. #endif
  513. for (i = 0; i < context->name_count; i++)
  514. if (context->names[i].name)
  515. __putname(context->names[i].name);
  516. context->name_count = 0;
  517. if (context->pwd)
  518. dput(context->pwd);
  519. if (context->pwdmnt)
  520. mntput(context->pwdmnt);
  521. context->pwd = NULL;
  522. context->pwdmnt = NULL;
  523. }
  524. static inline void audit_free_aux(struct audit_context *context)
  525. {
  526. struct audit_aux_data *aux;
  527. while ((aux = context->aux)) {
  528. if (aux->type == AUDIT_AVC_PATH) {
  529. struct audit_aux_data_path *axi = (void *)aux;
  530. dput(axi->dentry);
  531. mntput(axi->mnt);
  532. }
  533. context->aux = aux->next;
  534. kfree(aux);
  535. }
  536. }
  537. static inline void audit_zero_context(struct audit_context *context,
  538. enum audit_state state)
  539. {
  540. uid_t loginuid = context->loginuid;
  541. memset(context, 0, sizeof(*context));
  542. context->state = state;
  543. context->loginuid = loginuid;
  544. }
  545. static inline struct audit_context *audit_alloc_context(enum audit_state state)
  546. {
  547. struct audit_context *context;
  548. if (!(context = kmalloc(sizeof(*context), GFP_KERNEL)))
  549. return NULL;
  550. audit_zero_context(context, state);
  551. return context;
  552. }
  553. /* Filter on the task information and allocate a per-task audit context
  554. * if necessary. Doing so turns on system call auditing for the
  555. * specified task. This is called from copy_process, so no lock is
  556. * needed. */
  557. int audit_alloc(struct task_struct *tsk)
  558. {
  559. struct audit_context *context;
  560. enum audit_state state;
  561. if (likely(!audit_enabled))
  562. return 0; /* Return if not auditing. */
  563. state = audit_filter_task(tsk);
  564. if (likely(state == AUDIT_DISABLED))
  565. return 0;
  566. if (!(context = audit_alloc_context(state))) {
  567. audit_log_lost("out of memory in audit_alloc");
  568. return -ENOMEM;
  569. }
  570. /* Preserve login uid */
  571. context->loginuid = -1;
  572. if (current->audit_context)
  573. context->loginuid = current->audit_context->loginuid;
  574. tsk->audit_context = context;
  575. set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
  576. return 0;
  577. }
  578. static inline void audit_free_context(struct audit_context *context)
  579. {
  580. struct audit_context *previous;
  581. int count = 0;
  582. do {
  583. previous = context->previous;
  584. if (previous || (count && count < 10)) {
  585. ++count;
  586. printk(KERN_ERR "audit(:%d): major=%d name_count=%d:"
  587. " freeing multiple contexts (%d)\n",
  588. context->serial, context->major,
  589. context->name_count, count);
  590. }
  591. audit_free_names(context);
  592. audit_free_aux(context);
  593. kfree(context);
  594. context = previous;
  595. } while (context);
  596. if (count >= 10)
  597. printk(KERN_ERR "audit: freed %d contexts\n", count);
  598. }
  599. static void audit_log_task_info(struct audit_buffer *ab)
  600. {
  601. char name[sizeof(current->comm)];
  602. struct mm_struct *mm = current->mm;
  603. struct vm_area_struct *vma;
  604. get_task_comm(name, current);
  605. audit_log_format(ab, " comm=");
  606. audit_log_untrustedstring(ab, name);
  607. if (!mm)
  608. return;
  609. down_read(&mm->mmap_sem);
  610. vma = mm->mmap;
  611. while (vma) {
  612. if ((vma->vm_flags & VM_EXECUTABLE) &&
  613. vma->vm_file) {
  614. audit_log_d_path(ab, "exe=",
  615. vma->vm_file->f_dentry,
  616. vma->vm_file->f_vfsmnt);
  617. break;
  618. }
  619. vma = vma->vm_next;
  620. }
  621. up_read(&mm->mmap_sem);
  622. }
  623. static void audit_log_exit(struct audit_context *context)
  624. {
  625. int i;
  626. struct audit_buffer *ab;
  627. struct audit_aux_data *aux;
  628. ab = audit_log_start(context, AUDIT_SYSCALL);
  629. if (!ab)
  630. return; /* audit_panic has been called */
  631. audit_log_format(ab, "arch=%x syscall=%d",
  632. context->arch, context->major);
  633. if (context->personality != PER_LINUX)
  634. audit_log_format(ab, " per=%lx", context->personality);
  635. if (context->return_valid)
  636. audit_log_format(ab, " success=%s exit=%ld",
  637. (context->return_valid==AUDITSC_SUCCESS)?"yes":"no",
  638. context->return_code);
  639. audit_log_format(ab,
  640. " a0=%lx a1=%lx a2=%lx a3=%lx items=%d"
  641. " pid=%d auid=%u uid=%u gid=%u"
  642. " euid=%u suid=%u fsuid=%u"
  643. " egid=%u sgid=%u fsgid=%u",
  644. context->argv[0],
  645. context->argv[1],
  646. context->argv[2],
  647. context->argv[3],
  648. context->name_count,
  649. context->pid,
  650. context->loginuid,
  651. context->uid,
  652. context->gid,
  653. context->euid, context->suid, context->fsuid,
  654. context->egid, context->sgid, context->fsgid);
  655. audit_log_task_info(ab);
  656. audit_log_end(ab);
  657. for (aux = context->aux; aux; aux = aux->next) {
  658. ab = audit_log_start(context, aux->type);
  659. if (!ab)
  660. continue; /* audit_panic has been called */
  661. switch (aux->type) {
  662. case AUDIT_IPC: {
  663. struct audit_aux_data_ipcctl *axi = (void *)aux;
  664. audit_log_format(ab,
  665. " qbytes=%lx iuid=%u igid=%u mode=%x",
  666. axi->qbytes, axi->uid, axi->gid, axi->mode);
  667. break; }
  668. case AUDIT_SOCKETCALL: {
  669. int i;
  670. struct audit_aux_data_socketcall *axs = (void *)aux;
  671. audit_log_format(ab, "nargs=%d", axs->nargs);
  672. for (i=0; i<axs->nargs; i++)
  673. audit_log_format(ab, " a%d=%lx", i, axs->args[i]);
  674. break; }
  675. case AUDIT_SOCKADDR: {
  676. struct audit_aux_data_sockaddr *axs = (void *)aux;
  677. audit_log_format(ab, "saddr=");
  678. audit_log_hex(ab, axs->a, axs->len);
  679. break; }
  680. case AUDIT_AVC_PATH: {
  681. struct audit_aux_data_path *axi = (void *)aux;
  682. audit_log_d_path(ab, "path=", axi->dentry, axi->mnt);
  683. break; }
  684. }
  685. audit_log_end(ab);
  686. }
  687. if (context->pwd && context->pwdmnt) {
  688. ab = audit_log_start(context, AUDIT_CWD);
  689. if (ab) {
  690. audit_log_d_path(ab, "cwd=", context->pwd, context->pwdmnt);
  691. audit_log_end(ab);
  692. }
  693. }
  694. for (i = 0; i < context->name_count; i++) {
  695. ab = audit_log_start(context, AUDIT_PATH);
  696. if (!ab)
  697. continue; /* audit_panic has been called */
  698. audit_log_format(ab, "item=%d", i);
  699. if (context->names[i].name) {
  700. audit_log_format(ab, " name=");
  701. audit_log_untrustedstring(ab, context->names[i].name);
  702. }
  703. if (context->names[i].ino != (unsigned long)-1)
  704. audit_log_format(ab, " inode=%lu dev=%02x:%02x mode=%#o"
  705. " ouid=%u ogid=%u rdev=%02x:%02x",
  706. context->names[i].ino,
  707. MAJOR(context->names[i].dev),
  708. MINOR(context->names[i].dev),
  709. context->names[i].mode,
  710. context->names[i].uid,
  711. context->names[i].gid,
  712. MAJOR(context->names[i].rdev),
  713. MINOR(context->names[i].rdev));
  714. audit_log_end(ab);
  715. }
  716. }
  717. /* Free a per-task audit context. Called from copy_process and
  718. * __put_task_struct. */
  719. void audit_free(struct task_struct *tsk)
  720. {
  721. struct audit_context *context;
  722. task_lock(tsk);
  723. context = audit_get_context(tsk, 0, 0);
  724. task_unlock(tsk);
  725. if (likely(!context))
  726. return;
  727. /* Check for system calls that do not go through the exit
  728. * function (e.g., exit_group), then free context block. */
  729. if (context->in_syscall && context->auditable && context->pid != audit_pid)
  730. audit_log_exit(context);
  731. audit_free_context(context);
  732. }
  733. /* Fill in audit context at syscall entry. This only happens if the
  734. * audit context was created when the task was created and the state or
  735. * filters demand the audit context be built. If the state from the
  736. * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT,
  737. * then the record will be written at syscall exit time (otherwise, it
  738. * will only be written if another part of the kernel requests that it
  739. * be written). */
  740. void audit_syscall_entry(struct task_struct *tsk, int arch, int major,
  741. unsigned long a1, unsigned long a2,
  742. unsigned long a3, unsigned long a4)
  743. {
  744. struct audit_context *context = tsk->audit_context;
  745. enum audit_state state;
  746. BUG_ON(!context);
  747. /* This happens only on certain architectures that make system
  748. * calls in kernel_thread via the entry.S interface, instead of
  749. * with direct calls. (If you are porting to a new
  750. * architecture, hitting this condition can indicate that you
  751. * got the _exit/_leave calls backward in entry.S.)
  752. *
  753. * i386 no
  754. * x86_64 no
  755. * ppc64 yes (see arch/ppc64/kernel/misc.S)
  756. *
  757. * This also happens with vm86 emulation in a non-nested manner
  758. * (entries without exits), so this case must be caught.
  759. */
  760. if (context->in_syscall) {
  761. struct audit_context *newctx;
  762. #if defined(__NR_vm86) && defined(__NR_vm86old)
  763. /* vm86 mode should only be entered once */
  764. if (major == __NR_vm86 || major == __NR_vm86old)
  765. return;
  766. #endif
  767. #if AUDIT_DEBUG
  768. printk(KERN_ERR
  769. "audit(:%d) pid=%d in syscall=%d;"
  770. " entering syscall=%d\n",
  771. context->serial, tsk->pid, context->major, major);
  772. #endif
  773. newctx = audit_alloc_context(context->state);
  774. if (newctx) {
  775. newctx->previous = context;
  776. context = newctx;
  777. tsk->audit_context = newctx;
  778. } else {
  779. /* If we can't alloc a new context, the best we
  780. * can do is to leak memory (any pending putname
  781. * will be lost). The only other alternative is
  782. * to abandon auditing. */
  783. audit_zero_context(context, context->state);
  784. }
  785. }
  786. BUG_ON(context->in_syscall || context->name_count);
  787. if (!audit_enabled)
  788. return;
  789. context->arch = arch;
  790. context->major = major;
  791. context->argv[0] = a1;
  792. context->argv[1] = a2;
  793. context->argv[2] = a3;
  794. context->argv[3] = a4;
  795. state = context->state;
  796. if (state == AUDIT_SETUP_CONTEXT || state == AUDIT_BUILD_CONTEXT)
  797. state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_ENTRY]);
  798. if (likely(state == AUDIT_DISABLED))
  799. return;
  800. context->serial = audit_serial();
  801. context->ctime = CURRENT_TIME;
  802. context->in_syscall = 1;
  803. context->auditable = !!(state == AUDIT_RECORD_CONTEXT);
  804. }
  805. /* Tear down after system call. If the audit context has been marked as
  806. * auditable (either because of the AUDIT_RECORD_CONTEXT state from
  807. * filtering, or because some other part of the kernel write an audit
  808. * message), then write out the syscall information. In call cases,
  809. * free the names stored from getname(). */
  810. void audit_syscall_exit(struct task_struct *tsk, int valid, long return_code)
  811. {
  812. struct audit_context *context;
  813. get_task_struct(tsk);
  814. task_lock(tsk);
  815. context = audit_get_context(tsk, valid, return_code);
  816. task_unlock(tsk);
  817. /* Not having a context here is ok, since the parent may have
  818. * called __put_task_struct. */
  819. if (likely(!context))
  820. return;
  821. if (context->in_syscall && context->auditable && context->pid != audit_pid)
  822. audit_log_exit(context);
  823. context->in_syscall = 0;
  824. context->auditable = 0;
  825. if (context->previous) {
  826. struct audit_context *new_context = context->previous;
  827. context->previous = NULL;
  828. audit_free_context(context);
  829. tsk->audit_context = new_context;
  830. } else {
  831. audit_free_names(context);
  832. audit_free_aux(context);
  833. audit_zero_context(context, context->state);
  834. tsk->audit_context = context;
  835. }
  836. put_task_struct(tsk);
  837. }
  838. /* Add a name to the list. Called from fs/namei.c:getname(). */
  839. void audit_getname(const char *name)
  840. {
  841. struct audit_context *context = current->audit_context;
  842. if (!context || IS_ERR(name) || !name)
  843. return;
  844. if (!context->in_syscall) {
  845. #if AUDIT_DEBUG == 2
  846. printk(KERN_ERR "%s:%d(:%d): ignoring getname(%p)\n",
  847. __FILE__, __LINE__, context->serial, name);
  848. dump_stack();
  849. #endif
  850. return;
  851. }
  852. BUG_ON(context->name_count >= AUDIT_NAMES);
  853. context->names[context->name_count].name = name;
  854. context->names[context->name_count].ino = (unsigned long)-1;
  855. ++context->name_count;
  856. if (!context->pwd) {
  857. read_lock(&current->fs->lock);
  858. context->pwd = dget(current->fs->pwd);
  859. context->pwdmnt = mntget(current->fs->pwdmnt);
  860. read_unlock(&current->fs->lock);
  861. }
  862. }
  863. /* Intercept a putname request. Called from
  864. * include/linux/fs.h:putname(). If we have stored the name from
  865. * getname in the audit context, then we delay the putname until syscall
  866. * exit. */
  867. void audit_putname(const char *name)
  868. {
  869. struct audit_context *context = current->audit_context;
  870. BUG_ON(!context);
  871. if (!context->in_syscall) {
  872. #if AUDIT_DEBUG == 2
  873. printk(KERN_ERR "%s:%d(:%d): __putname(%p)\n",
  874. __FILE__, __LINE__, context->serial, name);
  875. if (context->name_count) {
  876. int i;
  877. for (i = 0; i < context->name_count; i++)
  878. printk(KERN_ERR "name[%d] = %p = %s\n", i,
  879. context->names[i].name,
  880. context->names[i].name);
  881. }
  882. #endif
  883. __putname(name);
  884. }
  885. #if AUDIT_DEBUG
  886. else {
  887. ++context->put_count;
  888. if (context->put_count > context->name_count) {
  889. printk(KERN_ERR "%s:%d(:%d): major=%d"
  890. " in_syscall=%d putname(%p) name_count=%d"
  891. " put_count=%d\n",
  892. __FILE__, __LINE__,
  893. context->serial, context->major,
  894. context->in_syscall, name, context->name_count,
  895. context->put_count);
  896. dump_stack();
  897. }
  898. }
  899. #endif
  900. }
  901. /* Store the inode and device from a lookup. Called from
  902. * fs/namei.c:path_lookup(). */
  903. void audit_inode(const char *name, const struct inode *inode)
  904. {
  905. int idx;
  906. struct audit_context *context = current->audit_context;
  907. if (!context->in_syscall)
  908. return;
  909. if (context->name_count
  910. && context->names[context->name_count-1].name
  911. && context->names[context->name_count-1].name == name)
  912. idx = context->name_count - 1;
  913. else if (context->name_count > 1
  914. && context->names[context->name_count-2].name
  915. && context->names[context->name_count-2].name == name)
  916. idx = context->name_count - 2;
  917. else {
  918. /* FIXME: how much do we care about inodes that have no
  919. * associated name? */
  920. if (context->name_count >= AUDIT_NAMES - AUDIT_NAMES_RESERVED)
  921. return;
  922. idx = context->name_count++;
  923. context->names[idx].name = NULL;
  924. #if AUDIT_DEBUG
  925. ++context->ino_count;
  926. #endif
  927. }
  928. context->names[idx].ino = inode->i_ino;
  929. context->names[idx].dev = inode->i_sb->s_dev;
  930. context->names[idx].mode = inode->i_mode;
  931. context->names[idx].uid = inode->i_uid;
  932. context->names[idx].gid = inode->i_gid;
  933. context->names[idx].rdev = inode->i_rdev;
  934. }
  935. void auditsc_get_stamp(struct audit_context *ctx,
  936. struct timespec *t, unsigned int *serial)
  937. {
  938. t->tv_sec = ctx->ctime.tv_sec;
  939. t->tv_nsec = ctx->ctime.tv_nsec;
  940. *serial = ctx->serial;
  941. ctx->auditable = 1;
  942. }
  943. int audit_set_loginuid(struct task_struct *task, uid_t loginuid)
  944. {
  945. if (task->audit_context) {
  946. struct audit_buffer *ab;
  947. ab = audit_log_start(NULL, AUDIT_LOGIN);
  948. if (ab) {
  949. audit_log_format(ab, "login pid=%d uid=%u "
  950. "old auid=%u new auid=%u",
  951. task->pid, task->uid,
  952. task->audit_context->loginuid, loginuid);
  953. audit_log_end(ab);
  954. }
  955. task->audit_context->loginuid = loginuid;
  956. }
  957. return 0;
  958. }
  959. uid_t audit_get_loginuid(struct audit_context *ctx)
  960. {
  961. return ctx ? ctx->loginuid : -1;
  962. }
  963. int audit_ipc_perms(unsigned long qbytes, uid_t uid, gid_t gid, mode_t mode)
  964. {
  965. struct audit_aux_data_ipcctl *ax;
  966. struct audit_context *context = current->audit_context;
  967. if (likely(!context))
  968. return 0;
  969. ax = kmalloc(sizeof(*ax), GFP_KERNEL);
  970. if (!ax)
  971. return -ENOMEM;
  972. ax->qbytes = qbytes;
  973. ax->uid = uid;
  974. ax->gid = gid;
  975. ax->mode = mode;
  976. ax->d.type = AUDIT_IPC;
  977. ax->d.next = context->aux;
  978. context->aux = (void *)ax;
  979. return 0;
  980. }
  981. int audit_socketcall(int nargs, unsigned long *args)
  982. {
  983. struct audit_aux_data_socketcall *ax;
  984. struct audit_context *context = current->audit_context;
  985. if (likely(!context))
  986. return 0;
  987. ax = kmalloc(sizeof(*ax) + nargs * sizeof(unsigned long), GFP_KERNEL);
  988. if (!ax)
  989. return -ENOMEM;
  990. ax->nargs = nargs;
  991. memcpy(ax->args, args, nargs * sizeof(unsigned long));
  992. ax->d.type = AUDIT_SOCKETCALL;
  993. ax->d.next = context->aux;
  994. context->aux = (void *)ax;
  995. return 0;
  996. }
  997. int audit_sockaddr(int len, void *a)
  998. {
  999. struct audit_aux_data_sockaddr *ax;
  1000. struct audit_context *context = current->audit_context;
  1001. if (likely(!context))
  1002. return 0;
  1003. ax = kmalloc(sizeof(*ax) + len, GFP_KERNEL);
  1004. if (!ax)
  1005. return -ENOMEM;
  1006. ax->len = len;
  1007. memcpy(ax->a, a, len);
  1008. ax->d.type = AUDIT_SOCKADDR;
  1009. ax->d.next = context->aux;
  1010. context->aux = (void *)ax;
  1011. return 0;
  1012. }
  1013. int audit_avc_path(struct dentry *dentry, struct vfsmount *mnt)
  1014. {
  1015. struct audit_aux_data_path *ax;
  1016. struct audit_context *context = current->audit_context;
  1017. if (likely(!context))
  1018. return 0;
  1019. ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
  1020. if (!ax)
  1021. return -ENOMEM;
  1022. ax->dentry = dget(dentry);
  1023. ax->mnt = mntget(mnt);
  1024. ax->d.type = AUDIT_AVC_PATH;
  1025. ax->d.next = context->aux;
  1026. context->aux = (void *)ax;
  1027. return 0;
  1028. }
  1029. void audit_signal_info(int sig, struct task_struct *t)
  1030. {
  1031. extern pid_t audit_sig_pid;
  1032. extern uid_t audit_sig_uid;
  1033. if (unlikely(audit_pid && t->pid == audit_pid)) {
  1034. if (sig == SIGTERM || sig == SIGHUP) {
  1035. struct audit_context *ctx = current->audit_context;
  1036. audit_sig_pid = current->pid;
  1037. if (ctx)
  1038. audit_sig_uid = ctx->loginuid;
  1039. else
  1040. audit_sig_uid = current->uid;
  1041. }
  1042. }
  1043. }