auditsc.c 36 KB

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