auditsc.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328
  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. task_lock(tsk);
  630. context = audit_get_context(tsk, 0, 0);
  631. task_unlock(tsk);
  632. if (likely(!context))
  633. return;
  634. /* Check for system calls that do not go through the exit
  635. * function (e.g., exit_group), then free context block.
  636. * We use GFP_ATOMIC here because we might be doing this
  637. * in the context of the idle thread */
  638. if (context->in_syscall && context->auditable)
  639. audit_log_exit(context, GFP_ATOMIC);
  640. audit_free_context(context);
  641. }
  642. /**
  643. * audit_syscall_entry - fill in an audit record at syscall entry
  644. * @tsk: task being audited
  645. * @arch: architecture type
  646. * @major: major syscall type (function)
  647. * @a1: additional syscall register 1
  648. * @a2: additional syscall register 2
  649. * @a3: additional syscall register 3
  650. * @a4: additional syscall register 4
  651. *
  652. * Fill in audit context at syscall entry. This only happens if the
  653. * audit context was created when the task was created and the state or
  654. * filters demand the audit context be built. If the state from the
  655. * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT,
  656. * then the record will be written at syscall exit time (otherwise, it
  657. * will only be written if another part of the kernel requests that it
  658. * be written).
  659. */
  660. void audit_syscall_entry(struct task_struct *tsk, int arch, int major,
  661. unsigned long a1, unsigned long a2,
  662. unsigned long a3, unsigned long a4)
  663. {
  664. struct audit_context *context = tsk->audit_context;
  665. enum audit_state state;
  666. BUG_ON(!context);
  667. /*
  668. * This happens only on certain architectures that make system
  669. * calls in kernel_thread via the entry.S interface, instead of
  670. * with direct calls. (If you are porting to a new
  671. * architecture, hitting this condition can indicate that you
  672. * got the _exit/_leave calls backward in entry.S.)
  673. *
  674. * i386 no
  675. * x86_64 no
  676. * ppc64 yes (see arch/ppc64/kernel/misc.S)
  677. *
  678. * This also happens with vm86 emulation in a non-nested manner
  679. * (entries without exits), so this case must be caught.
  680. */
  681. if (context->in_syscall) {
  682. struct audit_context *newctx;
  683. #if AUDIT_DEBUG
  684. printk(KERN_ERR
  685. "audit(:%d) pid=%d in syscall=%d;"
  686. " entering syscall=%d\n",
  687. context->serial, tsk->pid, context->major, major);
  688. #endif
  689. newctx = audit_alloc_context(context->state);
  690. if (newctx) {
  691. newctx->previous = context;
  692. context = newctx;
  693. tsk->audit_context = newctx;
  694. } else {
  695. /* If we can't alloc a new context, the best we
  696. * can do is to leak memory (any pending putname
  697. * will be lost). The only other alternative is
  698. * to abandon auditing. */
  699. audit_zero_context(context, context->state);
  700. }
  701. }
  702. BUG_ON(context->in_syscall || context->name_count);
  703. if (!audit_enabled)
  704. return;
  705. context->arch = arch;
  706. context->major = major;
  707. context->argv[0] = a1;
  708. context->argv[1] = a2;
  709. context->argv[2] = a3;
  710. context->argv[3] = a4;
  711. state = context->state;
  712. if (state == AUDIT_SETUP_CONTEXT || state == AUDIT_BUILD_CONTEXT)
  713. state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_ENTRY]);
  714. if (likely(state == AUDIT_DISABLED))
  715. return;
  716. context->serial = 0;
  717. context->ctime = CURRENT_TIME;
  718. context->in_syscall = 1;
  719. context->auditable = !!(state == AUDIT_RECORD_CONTEXT);
  720. }
  721. /**
  722. * audit_syscall_exit - deallocate audit context after a system call
  723. * @tsk: task being audited
  724. * @valid: success/failure flag
  725. * @return_code: syscall return value
  726. *
  727. * Tear down after system call. If the audit context has been marked as
  728. * auditable (either because of the AUDIT_RECORD_CONTEXT state from
  729. * filtering, or because some other part of the kernel write an audit
  730. * message), then write out the syscall information. In call cases,
  731. * free the names stored from getname().
  732. */
  733. void audit_syscall_exit(struct task_struct *tsk, int valid, long return_code)
  734. {
  735. struct audit_context *context;
  736. get_task_struct(tsk);
  737. task_lock(tsk);
  738. context = audit_get_context(tsk, valid, return_code);
  739. task_unlock(tsk);
  740. /* Not having a context here is ok, since the parent may have
  741. * called __put_task_struct. */
  742. if (likely(!context))
  743. goto out;
  744. if (context->in_syscall && context->auditable)
  745. audit_log_exit(context, GFP_KERNEL);
  746. context->in_syscall = 0;
  747. context->auditable = 0;
  748. if (context->previous) {
  749. struct audit_context *new_context = context->previous;
  750. context->previous = NULL;
  751. audit_free_context(context);
  752. tsk->audit_context = new_context;
  753. } else {
  754. audit_free_names(context);
  755. audit_free_aux(context);
  756. tsk->audit_context = context;
  757. }
  758. out:
  759. put_task_struct(tsk);
  760. }
  761. /**
  762. * audit_getname - add a name to the list
  763. * @name: name to add
  764. *
  765. * Add a name to the list of audit names for this context.
  766. * Called from fs/namei.c:getname().
  767. */
  768. void audit_getname(const char *name)
  769. {
  770. struct audit_context *context = current->audit_context;
  771. if (!context || IS_ERR(name) || !name)
  772. return;
  773. if (!context->in_syscall) {
  774. #if AUDIT_DEBUG == 2
  775. printk(KERN_ERR "%s:%d(:%d): ignoring getname(%p)\n",
  776. __FILE__, __LINE__, context->serial, name);
  777. dump_stack();
  778. #endif
  779. return;
  780. }
  781. BUG_ON(context->name_count >= AUDIT_NAMES);
  782. context->names[context->name_count].name = name;
  783. context->names[context->name_count].ino = (unsigned long)-1;
  784. ++context->name_count;
  785. if (!context->pwd) {
  786. read_lock(&current->fs->lock);
  787. context->pwd = dget(current->fs->pwd);
  788. context->pwdmnt = mntget(current->fs->pwdmnt);
  789. read_unlock(&current->fs->lock);
  790. }
  791. }
  792. /* audit_putname - intercept a putname request
  793. * @name: name to intercept and delay for putname
  794. *
  795. * If we have stored the name from getname in the audit context,
  796. * then we delay the putname until syscall exit.
  797. * Called from include/linux/fs.h:putname().
  798. */
  799. void audit_putname(const char *name)
  800. {
  801. struct audit_context *context = current->audit_context;
  802. BUG_ON(!context);
  803. if (!context->in_syscall) {
  804. #if AUDIT_DEBUG == 2
  805. printk(KERN_ERR "%s:%d(:%d): __putname(%p)\n",
  806. __FILE__, __LINE__, context->serial, name);
  807. if (context->name_count) {
  808. int i;
  809. for (i = 0; i < context->name_count; i++)
  810. printk(KERN_ERR "name[%d] = %p = %s\n", i,
  811. context->names[i].name,
  812. context->names[i].name ?: "(null)");
  813. }
  814. #endif
  815. __putname(name);
  816. }
  817. #if AUDIT_DEBUG
  818. else {
  819. ++context->put_count;
  820. if (context->put_count > context->name_count) {
  821. printk(KERN_ERR "%s:%d(:%d): major=%d"
  822. " in_syscall=%d putname(%p) name_count=%d"
  823. " put_count=%d\n",
  824. __FILE__, __LINE__,
  825. context->serial, context->major,
  826. context->in_syscall, name, context->name_count,
  827. context->put_count);
  828. dump_stack();
  829. }
  830. }
  831. #endif
  832. }
  833. void audit_inode_context(int idx, const struct inode *inode)
  834. {
  835. struct audit_context *context = current->audit_context;
  836. const char *suffix = security_inode_xattr_getsuffix();
  837. char *ctx = NULL;
  838. int len = 0;
  839. if (!suffix)
  840. goto ret;
  841. len = security_inode_getsecurity(inode, suffix, NULL, 0, 0);
  842. if (len == -EOPNOTSUPP)
  843. goto ret;
  844. if (len < 0)
  845. goto error_path;
  846. ctx = kmalloc(len, GFP_KERNEL);
  847. if (!ctx)
  848. goto error_path;
  849. len = security_inode_getsecurity(inode, suffix, ctx, len, 0);
  850. if (len < 0)
  851. goto error_path;
  852. kfree(context->names[idx].ctx);
  853. context->names[idx].ctx = ctx;
  854. goto ret;
  855. error_path:
  856. if (ctx)
  857. kfree(ctx);
  858. audit_panic("error in audit_inode_context");
  859. ret:
  860. return;
  861. }
  862. /**
  863. * audit_inode - store the inode and device from a lookup
  864. * @name: name being audited
  865. * @inode: inode being audited
  866. * @flags: lookup flags (as used in path_lookup())
  867. *
  868. * Called from fs/namei.c:path_lookup().
  869. */
  870. void __audit_inode(const char *name, const struct inode *inode, unsigned flags)
  871. {
  872. int idx;
  873. struct audit_context *context = current->audit_context;
  874. if (!context->in_syscall)
  875. return;
  876. if (context->name_count
  877. && context->names[context->name_count-1].name
  878. && context->names[context->name_count-1].name == name)
  879. idx = context->name_count - 1;
  880. else if (context->name_count > 1
  881. && context->names[context->name_count-2].name
  882. && context->names[context->name_count-2].name == name)
  883. idx = context->name_count - 2;
  884. else {
  885. /* FIXME: how much do we care about inodes that have no
  886. * associated name? */
  887. if (context->name_count >= AUDIT_NAMES - AUDIT_NAMES_RESERVED)
  888. return;
  889. idx = context->name_count++;
  890. context->names[idx].name = NULL;
  891. #if AUDIT_DEBUG
  892. ++context->ino_count;
  893. #endif
  894. }
  895. context->names[idx].dev = inode->i_sb->s_dev;
  896. context->names[idx].mode = inode->i_mode;
  897. context->names[idx].uid = inode->i_uid;
  898. context->names[idx].gid = inode->i_gid;
  899. context->names[idx].rdev = inode->i_rdev;
  900. audit_inode_context(idx, inode);
  901. if ((flags & LOOKUP_PARENT) && (strcmp(name, "/") != 0) &&
  902. (strcmp(name, ".") != 0)) {
  903. context->names[idx].ino = (unsigned long)-1;
  904. context->names[idx].pino = inode->i_ino;
  905. } else {
  906. context->names[idx].ino = inode->i_ino;
  907. context->names[idx].pino = (unsigned long)-1;
  908. }
  909. }
  910. /**
  911. * audit_inode_child - collect inode info for created/removed objects
  912. * @dname: inode's dentry name
  913. * @inode: inode being audited
  914. * @pino: inode number of dentry parent
  915. *
  916. * For syscalls that create or remove filesystem objects, audit_inode
  917. * can only collect information for the filesystem object's parent.
  918. * This call updates the audit context with the child's information.
  919. * Syscalls that create a new filesystem object must be hooked after
  920. * the object is created. Syscalls that remove a filesystem object
  921. * must be hooked prior, in order to capture the target inode during
  922. * unsuccessful attempts.
  923. */
  924. void __audit_inode_child(const char *dname, const struct inode *inode,
  925. unsigned long pino)
  926. {
  927. int idx;
  928. struct audit_context *context = current->audit_context;
  929. if (!context->in_syscall)
  930. return;
  931. /* determine matching parent */
  932. if (dname)
  933. for (idx = 0; idx < context->name_count; idx++)
  934. if (context->names[idx].pino == pino) {
  935. const char *n;
  936. const char *name = context->names[idx].name;
  937. int dlen = strlen(dname);
  938. int nlen = name ? strlen(name) : 0;
  939. if (nlen < dlen)
  940. continue;
  941. /* disregard trailing slashes */
  942. n = name + nlen - 1;
  943. while ((*n == '/') && (n > name))
  944. n--;
  945. /* find last path component */
  946. n = n - dlen + 1;
  947. if (n < name)
  948. continue;
  949. else if (n > name) {
  950. if (*--n != '/')
  951. continue;
  952. else
  953. n++;
  954. }
  955. if (strncmp(n, dname, dlen) == 0)
  956. goto update_context;
  957. }
  958. /* catch-all in case match not found */
  959. idx = context->name_count++;
  960. context->names[idx].name = NULL;
  961. context->names[idx].pino = pino;
  962. #if AUDIT_DEBUG
  963. context->ino_count++;
  964. #endif
  965. update_context:
  966. if (inode) {
  967. context->names[idx].ino = inode->i_ino;
  968. context->names[idx].dev = inode->i_sb->s_dev;
  969. context->names[idx].mode = inode->i_mode;
  970. context->names[idx].uid = inode->i_uid;
  971. context->names[idx].gid = inode->i_gid;
  972. context->names[idx].rdev = inode->i_rdev;
  973. audit_inode_context(idx, inode);
  974. }
  975. }
  976. /**
  977. * auditsc_get_stamp - get local copies of audit_context values
  978. * @ctx: audit_context for the task
  979. * @t: timespec to store time recorded in the audit_context
  980. * @serial: serial value that is recorded in the audit_context
  981. *
  982. * Also sets the context as auditable.
  983. */
  984. void auditsc_get_stamp(struct audit_context *ctx,
  985. struct timespec *t, unsigned int *serial)
  986. {
  987. if (!ctx->serial)
  988. ctx->serial = audit_serial();
  989. t->tv_sec = ctx->ctime.tv_sec;
  990. t->tv_nsec = ctx->ctime.tv_nsec;
  991. *serial = ctx->serial;
  992. ctx->auditable = 1;
  993. }
  994. /**
  995. * audit_set_loginuid - set a task's audit_context loginuid
  996. * @task: task whose audit context is being modified
  997. * @loginuid: loginuid value
  998. *
  999. * Returns 0.
  1000. *
  1001. * Called (set) from fs/proc/base.c::proc_loginuid_write().
  1002. */
  1003. int audit_set_loginuid(struct task_struct *task, uid_t loginuid)
  1004. {
  1005. if (task->audit_context) {
  1006. struct audit_buffer *ab;
  1007. ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_LOGIN);
  1008. if (ab) {
  1009. audit_log_format(ab, "login pid=%d uid=%u "
  1010. "old auid=%u new auid=%u",
  1011. task->pid, task->uid,
  1012. task->audit_context->loginuid, loginuid);
  1013. audit_log_end(ab);
  1014. }
  1015. task->audit_context->loginuid = loginuid;
  1016. }
  1017. return 0;
  1018. }
  1019. /**
  1020. * audit_get_loginuid - get the loginuid for an audit_context
  1021. * @ctx: the audit_context
  1022. *
  1023. * Returns the context's loginuid or -1 if @ctx is NULL.
  1024. */
  1025. uid_t audit_get_loginuid(struct audit_context *ctx)
  1026. {
  1027. return ctx ? ctx->loginuid : -1;
  1028. }
  1029. static char *audit_ipc_context(struct kern_ipc_perm *ipcp)
  1030. {
  1031. struct audit_context *context = current->audit_context;
  1032. char *ctx = NULL;
  1033. int len = 0;
  1034. if (likely(!context))
  1035. return NULL;
  1036. len = security_ipc_getsecurity(ipcp, NULL, 0);
  1037. if (len == -EOPNOTSUPP)
  1038. goto ret;
  1039. if (len < 0)
  1040. goto error_path;
  1041. ctx = kmalloc(len, GFP_ATOMIC);
  1042. if (!ctx)
  1043. goto error_path;
  1044. len = security_ipc_getsecurity(ipcp, ctx, len);
  1045. if (len < 0)
  1046. goto error_path;
  1047. return ctx;
  1048. error_path:
  1049. kfree(ctx);
  1050. audit_panic("error in audit_ipc_context");
  1051. ret:
  1052. return NULL;
  1053. }
  1054. /**
  1055. * audit_ipc_perms - record audit data for ipc
  1056. * @qbytes: msgq bytes
  1057. * @uid: msgq user id
  1058. * @gid: msgq group id
  1059. * @mode: msgq mode (permissions)
  1060. *
  1061. * Returns 0 for success or NULL context or < 0 on error.
  1062. */
  1063. int audit_ipc_perms(unsigned long qbytes, uid_t uid, gid_t gid, mode_t mode, struct kern_ipc_perm *ipcp)
  1064. {
  1065. struct audit_aux_data_ipcctl *ax;
  1066. struct audit_context *context = current->audit_context;
  1067. if (likely(!context))
  1068. return 0;
  1069. ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
  1070. if (!ax)
  1071. return -ENOMEM;
  1072. ax->qbytes = qbytes;
  1073. ax->uid = uid;
  1074. ax->gid = gid;
  1075. ax->mode = mode;
  1076. ax->ctx = audit_ipc_context(ipcp);
  1077. ax->d.type = AUDIT_IPC;
  1078. ax->d.next = context->aux;
  1079. context->aux = (void *)ax;
  1080. return 0;
  1081. }
  1082. /**
  1083. * audit_socketcall - record audit data for sys_socketcall
  1084. * @nargs: number of args
  1085. * @args: args array
  1086. *
  1087. * Returns 0 for success or NULL context or < 0 on error.
  1088. */
  1089. int audit_socketcall(int nargs, unsigned long *args)
  1090. {
  1091. struct audit_aux_data_socketcall *ax;
  1092. struct audit_context *context = current->audit_context;
  1093. if (likely(!context))
  1094. return 0;
  1095. ax = kmalloc(sizeof(*ax) + nargs * sizeof(unsigned long), GFP_KERNEL);
  1096. if (!ax)
  1097. return -ENOMEM;
  1098. ax->nargs = nargs;
  1099. memcpy(ax->args, args, nargs * sizeof(unsigned long));
  1100. ax->d.type = AUDIT_SOCKETCALL;
  1101. ax->d.next = context->aux;
  1102. context->aux = (void *)ax;
  1103. return 0;
  1104. }
  1105. /**
  1106. * audit_sockaddr - record audit data for sys_bind, sys_connect, sys_sendto
  1107. * @len: data length in user space
  1108. * @a: data address in kernel space
  1109. *
  1110. * Returns 0 for success or NULL context or < 0 on error.
  1111. */
  1112. int audit_sockaddr(int len, void *a)
  1113. {
  1114. struct audit_aux_data_sockaddr *ax;
  1115. struct audit_context *context = current->audit_context;
  1116. if (likely(!context))
  1117. return 0;
  1118. ax = kmalloc(sizeof(*ax) + len, GFP_KERNEL);
  1119. if (!ax)
  1120. return -ENOMEM;
  1121. ax->len = len;
  1122. memcpy(ax->a, a, len);
  1123. ax->d.type = AUDIT_SOCKADDR;
  1124. ax->d.next = context->aux;
  1125. context->aux = (void *)ax;
  1126. return 0;
  1127. }
  1128. /**
  1129. * audit_avc_path - record the granting or denial of permissions
  1130. * @dentry: dentry to record
  1131. * @mnt: mnt to record
  1132. *
  1133. * Returns 0 for success or NULL context or < 0 on error.
  1134. *
  1135. * Called from security/selinux/avc.c::avc_audit()
  1136. */
  1137. int audit_avc_path(struct dentry *dentry, struct vfsmount *mnt)
  1138. {
  1139. struct audit_aux_data_path *ax;
  1140. struct audit_context *context = current->audit_context;
  1141. if (likely(!context))
  1142. return 0;
  1143. ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
  1144. if (!ax)
  1145. return -ENOMEM;
  1146. ax->dentry = dget(dentry);
  1147. ax->mnt = mntget(mnt);
  1148. ax->d.type = AUDIT_AVC_PATH;
  1149. ax->d.next = context->aux;
  1150. context->aux = (void *)ax;
  1151. return 0;
  1152. }
  1153. /**
  1154. * audit_signal_info - record signal info for shutting down audit subsystem
  1155. * @sig: signal value
  1156. * @t: task being signaled
  1157. *
  1158. * If the audit subsystem is being terminated, record the task (pid)
  1159. * and uid that is doing that.
  1160. */
  1161. void audit_signal_info(int sig, struct task_struct *t)
  1162. {
  1163. extern pid_t audit_sig_pid;
  1164. extern uid_t audit_sig_uid;
  1165. if (unlikely(audit_pid && t->tgid == audit_pid)) {
  1166. if (sig == SIGTERM || sig == SIGHUP) {
  1167. struct audit_context *ctx = current->audit_context;
  1168. audit_sig_pid = current->pid;
  1169. if (ctx)
  1170. audit_sig_uid = ctx->loginuid;
  1171. else
  1172. audit_sig_uid = current->uid;
  1173. }
  1174. }
  1175. }