auditsc.c 34 KB

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