auditsc.c 34 KB

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