auditsc.c 34 KB

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