auditsc.c 36 KB

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