auditsc.c 36 KB

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