auditsc.c 31 KB

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