auditsc.c 37 KB

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