auditsc.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727
  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, 2006 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. * POSIX message queue support added by George Wilson <ltcgcw@us.ibm.com>,
  33. * 2006.
  34. *
  35. * The support of additional filter rules compares (>, <, >=, <=) was
  36. * added by Dustin Kirkland <dustin.kirkland@us.ibm.com>, 2005.
  37. *
  38. * Modified by Amy Griffis <amy.griffis@hp.com> to collect additional
  39. * filesystem information.
  40. *
  41. * Subject and object context labeling support added by <danjones@us.ibm.com>
  42. * and <dustin.kirkland@us.ibm.com> for LSPP certification compliance.
  43. */
  44. #include <linux/init.h>
  45. #include <asm/types.h>
  46. #include <asm/atomic.h>
  47. #include <asm/types.h>
  48. #include <linux/fs.h>
  49. #include <linux/namei.h>
  50. #include <linux/mm.h>
  51. #include <linux/module.h>
  52. #include <linux/mount.h>
  53. #include <linux/socket.h>
  54. #include <linux/mqueue.h>
  55. #include <linux/audit.h>
  56. #include <linux/personality.h>
  57. #include <linux/time.h>
  58. #include <linux/netlink.h>
  59. #include <linux/compiler.h>
  60. #include <asm/unistd.h>
  61. #include <linux/security.h>
  62. #include <linux/list.h>
  63. #include <linux/tty.h>
  64. #include <linux/selinux.h>
  65. #include <linux/binfmts.h>
  66. #include <linux/syscalls.h>
  67. #include "audit.h"
  68. extern struct list_head audit_filter_list[];
  69. /* No syscall auditing will take place unless audit_enabled != 0. */
  70. extern int audit_enabled;
  71. /* AUDIT_NAMES is the number of slots we reserve in the audit_context
  72. * for saving names from getname(). */
  73. #define AUDIT_NAMES 20
  74. /* AUDIT_NAMES_RESERVED is the number of slots we reserve in the
  75. * audit_context from being used for nameless inodes from
  76. * path_lookup. */
  77. #define AUDIT_NAMES_RESERVED 7
  78. /* Indicates that audit should log the full pathname. */
  79. #define AUDIT_NAME_FULL -1
  80. /* When fs/namei.c:getname() is called, we store the pointer in name and
  81. * we don't let putname() free it (instead we free all of the saved
  82. * pointers at syscall exit time).
  83. *
  84. * Further, in fs/namei.c:path_lookup() we store the inode and device. */
  85. struct audit_names {
  86. const char *name;
  87. int name_len; /* number of name's characters to log */
  88. unsigned name_put; /* call __putname() for this name */
  89. unsigned long ino;
  90. dev_t dev;
  91. umode_t mode;
  92. uid_t uid;
  93. gid_t gid;
  94. dev_t rdev;
  95. u32 osid;
  96. };
  97. struct audit_aux_data {
  98. struct audit_aux_data *next;
  99. int type;
  100. };
  101. #define AUDIT_AUX_IPCPERM 0
  102. struct audit_aux_data_mq_open {
  103. struct audit_aux_data d;
  104. int oflag;
  105. mode_t mode;
  106. struct mq_attr attr;
  107. };
  108. struct audit_aux_data_mq_sendrecv {
  109. struct audit_aux_data d;
  110. mqd_t mqdes;
  111. size_t msg_len;
  112. unsigned int msg_prio;
  113. struct timespec abs_timeout;
  114. };
  115. struct audit_aux_data_mq_notify {
  116. struct audit_aux_data d;
  117. mqd_t mqdes;
  118. struct sigevent notification;
  119. };
  120. struct audit_aux_data_mq_getsetattr {
  121. struct audit_aux_data d;
  122. mqd_t mqdes;
  123. struct mq_attr mqstat;
  124. };
  125. struct audit_aux_data_ipcctl {
  126. struct audit_aux_data d;
  127. struct ipc_perm p;
  128. unsigned long qbytes;
  129. uid_t uid;
  130. gid_t gid;
  131. mode_t mode;
  132. u32 osid;
  133. };
  134. struct audit_aux_data_execve {
  135. struct audit_aux_data d;
  136. int argc;
  137. int envc;
  138. char mem[0];
  139. };
  140. struct audit_aux_data_socketcall {
  141. struct audit_aux_data d;
  142. int nargs;
  143. unsigned long args[0];
  144. };
  145. struct audit_aux_data_sockaddr {
  146. struct audit_aux_data d;
  147. int len;
  148. char a[0];
  149. };
  150. struct audit_aux_data_path {
  151. struct audit_aux_data d;
  152. struct dentry *dentry;
  153. struct vfsmount *mnt;
  154. };
  155. /* The per-task audit context. */
  156. struct audit_context {
  157. int in_syscall; /* 1 if task is in a syscall */
  158. enum audit_state state;
  159. unsigned int serial; /* serial number for record */
  160. struct timespec ctime; /* time of syscall entry */
  161. uid_t loginuid; /* login uid (identity) */
  162. int major; /* syscall number */
  163. unsigned long argv[4]; /* syscall arguments */
  164. int return_valid; /* return code is valid */
  165. long return_code;/* syscall return code */
  166. int auditable; /* 1 if record should be written */
  167. int name_count;
  168. struct audit_names names[AUDIT_NAMES];
  169. struct dentry * pwd;
  170. struct vfsmount * pwdmnt;
  171. struct audit_context *previous; /* For nested syscalls */
  172. struct audit_aux_data *aux;
  173. /* Save things to print about task_struct */
  174. pid_t pid, ppid;
  175. uid_t uid, euid, suid, fsuid;
  176. gid_t gid, egid, sgid, fsgid;
  177. unsigned long personality;
  178. int arch;
  179. #if AUDIT_DEBUG
  180. int put_count;
  181. int ino_count;
  182. #endif
  183. };
  184. /* Determine if any context name data matches a rule's watch data */
  185. /* Compare a task_struct with an audit_rule. Return 1 on match, 0
  186. * otherwise. */
  187. static int audit_filter_rules(struct task_struct *tsk,
  188. struct audit_krule *rule,
  189. struct audit_context *ctx,
  190. struct audit_names *name,
  191. enum audit_state *state)
  192. {
  193. int i, j, need_sid = 1;
  194. u32 sid;
  195. for (i = 0; i < rule->field_count; i++) {
  196. struct audit_field *f = &rule->fields[i];
  197. int result = 0;
  198. switch (f->type) {
  199. case AUDIT_PID:
  200. result = audit_comparator(tsk->pid, f->op, f->val);
  201. break;
  202. case AUDIT_PPID:
  203. if (ctx)
  204. result = audit_comparator(ctx->ppid, f->op, f->val);
  205. break;
  206. case AUDIT_UID:
  207. result = audit_comparator(tsk->uid, f->op, f->val);
  208. break;
  209. case AUDIT_EUID:
  210. result = audit_comparator(tsk->euid, f->op, f->val);
  211. break;
  212. case AUDIT_SUID:
  213. result = audit_comparator(tsk->suid, f->op, f->val);
  214. break;
  215. case AUDIT_FSUID:
  216. result = audit_comparator(tsk->fsuid, f->op, f->val);
  217. break;
  218. case AUDIT_GID:
  219. result = audit_comparator(tsk->gid, f->op, f->val);
  220. break;
  221. case AUDIT_EGID:
  222. result = audit_comparator(tsk->egid, f->op, f->val);
  223. break;
  224. case AUDIT_SGID:
  225. result = audit_comparator(tsk->sgid, f->op, f->val);
  226. break;
  227. case AUDIT_FSGID:
  228. result = audit_comparator(tsk->fsgid, f->op, f->val);
  229. break;
  230. case AUDIT_PERS:
  231. result = audit_comparator(tsk->personality, f->op, f->val);
  232. break;
  233. case AUDIT_ARCH:
  234. if (ctx)
  235. result = audit_comparator(ctx->arch, f->op, f->val);
  236. break;
  237. case AUDIT_EXIT:
  238. if (ctx && ctx->return_valid)
  239. result = audit_comparator(ctx->return_code, f->op, f->val);
  240. break;
  241. case AUDIT_SUCCESS:
  242. if (ctx && ctx->return_valid) {
  243. if (f->val)
  244. result = audit_comparator(ctx->return_valid, f->op, AUDITSC_SUCCESS);
  245. else
  246. result = audit_comparator(ctx->return_valid, f->op, AUDITSC_FAILURE);
  247. }
  248. break;
  249. case AUDIT_DEVMAJOR:
  250. if (name)
  251. result = audit_comparator(MAJOR(name->dev),
  252. f->op, f->val);
  253. else if (ctx) {
  254. for (j = 0; j < ctx->name_count; j++) {
  255. if (audit_comparator(MAJOR(ctx->names[j].dev), f->op, f->val)) {
  256. ++result;
  257. break;
  258. }
  259. }
  260. }
  261. break;
  262. case AUDIT_DEVMINOR:
  263. if (name)
  264. result = audit_comparator(MINOR(name->dev),
  265. f->op, f->val);
  266. else if (ctx) {
  267. for (j = 0; j < ctx->name_count; j++) {
  268. if (audit_comparator(MINOR(ctx->names[j].dev), f->op, f->val)) {
  269. ++result;
  270. break;
  271. }
  272. }
  273. }
  274. break;
  275. case AUDIT_INODE:
  276. if (name)
  277. result = (name->ino == f->val);
  278. else if (ctx) {
  279. for (j = 0; j < ctx->name_count; j++) {
  280. if (audit_comparator(ctx->names[j].ino, f->op, f->val)) {
  281. ++result;
  282. break;
  283. }
  284. }
  285. }
  286. break;
  287. case AUDIT_WATCH:
  288. if (name && rule->watch->ino != (unsigned long)-1)
  289. result = (name->dev == rule->watch->dev &&
  290. name->ino == rule->watch->ino);
  291. break;
  292. case AUDIT_LOGINUID:
  293. result = 0;
  294. if (ctx)
  295. result = audit_comparator(ctx->loginuid, f->op, f->val);
  296. break;
  297. case AUDIT_SE_USER:
  298. case AUDIT_SE_ROLE:
  299. case AUDIT_SE_TYPE:
  300. case AUDIT_SE_SEN:
  301. case AUDIT_SE_CLR:
  302. /* NOTE: this may return negative values indicating
  303. a temporary error. We simply treat this as a
  304. match for now to avoid losing information that
  305. may be wanted. An error message will also be
  306. logged upon error */
  307. if (f->se_rule) {
  308. if (need_sid) {
  309. selinux_task_ctxid(tsk, &sid);
  310. need_sid = 0;
  311. }
  312. result = selinux_audit_rule_match(sid, f->type,
  313. f->op,
  314. f->se_rule,
  315. ctx);
  316. }
  317. break;
  318. case AUDIT_ARG0:
  319. case AUDIT_ARG1:
  320. case AUDIT_ARG2:
  321. case AUDIT_ARG3:
  322. if (ctx)
  323. result = audit_comparator(ctx->argv[f->type-AUDIT_ARG0], f->op, f->val);
  324. break;
  325. }
  326. if (!result)
  327. return 0;
  328. }
  329. switch (rule->action) {
  330. case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
  331. case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
  332. }
  333. return 1;
  334. }
  335. /* At process creation time, we can determine if system-call auditing is
  336. * completely disabled for this task. Since we only have the task
  337. * structure at this point, we can only check uid and gid.
  338. */
  339. static enum audit_state audit_filter_task(struct task_struct *tsk)
  340. {
  341. struct audit_entry *e;
  342. enum audit_state state;
  343. rcu_read_lock();
  344. list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TASK], list) {
  345. if (audit_filter_rules(tsk, &e->rule, NULL, NULL, &state)) {
  346. rcu_read_unlock();
  347. return state;
  348. }
  349. }
  350. rcu_read_unlock();
  351. return AUDIT_BUILD_CONTEXT;
  352. }
  353. /* At syscall entry and exit time, this filter is called if the
  354. * audit_state is not low enough that auditing cannot take place, but is
  355. * also not high enough that we already know we have to write an audit
  356. * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT).
  357. */
  358. static enum audit_state audit_filter_syscall(struct task_struct *tsk,
  359. struct audit_context *ctx,
  360. struct list_head *list)
  361. {
  362. struct audit_entry *e;
  363. enum audit_state state;
  364. if (audit_pid && tsk->tgid == audit_pid)
  365. return AUDIT_DISABLED;
  366. rcu_read_lock();
  367. if (!list_empty(list)) {
  368. int word = AUDIT_WORD(ctx->major);
  369. int bit = AUDIT_BIT(ctx->major);
  370. list_for_each_entry_rcu(e, list, list) {
  371. if ((e->rule.mask[word] & bit) == bit &&
  372. audit_filter_rules(tsk, &e->rule, ctx, NULL,
  373. &state)) {
  374. rcu_read_unlock();
  375. return state;
  376. }
  377. }
  378. }
  379. rcu_read_unlock();
  380. return AUDIT_BUILD_CONTEXT;
  381. }
  382. /* At syscall exit time, this filter is called if any audit_names[] have been
  383. * collected during syscall processing. We only check rules in sublists at hash
  384. * buckets applicable to the inode numbers in audit_names[].
  385. * Regarding audit_state, same rules apply as for audit_filter_syscall().
  386. */
  387. enum audit_state audit_filter_inodes(struct task_struct *tsk,
  388. struct audit_context *ctx)
  389. {
  390. int i;
  391. struct audit_entry *e;
  392. enum audit_state state;
  393. if (audit_pid && tsk->tgid == audit_pid)
  394. return AUDIT_DISABLED;
  395. rcu_read_lock();
  396. for (i = 0; i < ctx->name_count; i++) {
  397. int word = AUDIT_WORD(ctx->major);
  398. int bit = AUDIT_BIT(ctx->major);
  399. struct audit_names *n = &ctx->names[i];
  400. int h = audit_hash_ino((u32)n->ino);
  401. struct list_head *list = &audit_inode_hash[h];
  402. if (list_empty(list))
  403. continue;
  404. list_for_each_entry_rcu(e, list, list) {
  405. if ((e->rule.mask[word] & bit) == bit &&
  406. audit_filter_rules(tsk, &e->rule, ctx, n, &state)) {
  407. rcu_read_unlock();
  408. return state;
  409. }
  410. }
  411. }
  412. rcu_read_unlock();
  413. return AUDIT_BUILD_CONTEXT;
  414. }
  415. void audit_set_auditable(struct audit_context *ctx)
  416. {
  417. ctx->auditable = 1;
  418. }
  419. static inline struct audit_context *audit_get_context(struct task_struct *tsk,
  420. int return_valid,
  421. int return_code)
  422. {
  423. struct audit_context *context = tsk->audit_context;
  424. if (likely(!context))
  425. return NULL;
  426. context->return_valid = return_valid;
  427. context->return_code = return_code;
  428. if (context->in_syscall && !context->auditable) {
  429. enum audit_state state;
  430. state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_EXIT]);
  431. if (state == AUDIT_RECORD_CONTEXT) {
  432. context->auditable = 1;
  433. goto get_context;
  434. }
  435. state = audit_filter_inodes(tsk, context);
  436. if (state == AUDIT_RECORD_CONTEXT)
  437. context->auditable = 1;
  438. }
  439. get_context:
  440. context->pid = tsk->pid;
  441. context->ppid = sys_getppid(); /* sic. tsk == current in all cases */
  442. context->uid = tsk->uid;
  443. context->gid = tsk->gid;
  444. context->euid = tsk->euid;
  445. context->suid = tsk->suid;
  446. context->fsuid = tsk->fsuid;
  447. context->egid = tsk->egid;
  448. context->sgid = tsk->sgid;
  449. context->fsgid = tsk->fsgid;
  450. context->personality = tsk->personality;
  451. tsk->audit_context = NULL;
  452. return context;
  453. }
  454. static inline void audit_free_names(struct audit_context *context)
  455. {
  456. int i;
  457. #if AUDIT_DEBUG == 2
  458. if (context->auditable
  459. ||context->put_count + context->ino_count != context->name_count) {
  460. printk(KERN_ERR "%s:%d(:%d): major=%d in_syscall=%d"
  461. " name_count=%d put_count=%d"
  462. " ino_count=%d [NOT freeing]\n",
  463. __FILE__, __LINE__,
  464. context->serial, context->major, context->in_syscall,
  465. context->name_count, context->put_count,
  466. context->ino_count);
  467. for (i = 0; i < context->name_count; i++) {
  468. printk(KERN_ERR "names[%d] = %p = %s\n", i,
  469. context->names[i].name,
  470. context->names[i].name ?: "(null)");
  471. }
  472. dump_stack();
  473. return;
  474. }
  475. #endif
  476. #if AUDIT_DEBUG
  477. context->put_count = 0;
  478. context->ino_count = 0;
  479. #endif
  480. for (i = 0; i < context->name_count; i++) {
  481. if (context->names[i].name && context->names[i].name_put)
  482. __putname(context->names[i].name);
  483. }
  484. context->name_count = 0;
  485. if (context->pwd)
  486. dput(context->pwd);
  487. if (context->pwdmnt)
  488. mntput(context->pwdmnt);
  489. context->pwd = NULL;
  490. context->pwdmnt = NULL;
  491. }
  492. static inline void audit_free_aux(struct audit_context *context)
  493. {
  494. struct audit_aux_data *aux;
  495. while ((aux = context->aux)) {
  496. if (aux->type == AUDIT_AVC_PATH) {
  497. struct audit_aux_data_path *axi = (void *)aux;
  498. dput(axi->dentry);
  499. mntput(axi->mnt);
  500. }
  501. context->aux = aux->next;
  502. kfree(aux);
  503. }
  504. }
  505. static inline void audit_zero_context(struct audit_context *context,
  506. enum audit_state state)
  507. {
  508. uid_t loginuid = context->loginuid;
  509. memset(context, 0, sizeof(*context));
  510. context->state = state;
  511. context->loginuid = loginuid;
  512. }
  513. static inline struct audit_context *audit_alloc_context(enum audit_state state)
  514. {
  515. struct audit_context *context;
  516. if (!(context = kmalloc(sizeof(*context), GFP_KERNEL)))
  517. return NULL;
  518. audit_zero_context(context, state);
  519. return context;
  520. }
  521. /**
  522. * audit_alloc - allocate an audit context block for a task
  523. * @tsk: task
  524. *
  525. * Filter on the task information and allocate a per-task audit context
  526. * if necessary. Doing so turns on system call auditing for the
  527. * specified task. This is called from copy_process, so no lock is
  528. * needed.
  529. */
  530. int audit_alloc(struct task_struct *tsk)
  531. {
  532. struct audit_context *context;
  533. enum audit_state state;
  534. if (likely(!audit_enabled))
  535. return 0; /* Return if not auditing. */
  536. state = audit_filter_task(tsk);
  537. if (likely(state == AUDIT_DISABLED))
  538. return 0;
  539. if (!(context = audit_alloc_context(state))) {
  540. audit_log_lost("out of memory in audit_alloc");
  541. return -ENOMEM;
  542. }
  543. /* Preserve login uid */
  544. context->loginuid = -1;
  545. if (current->audit_context)
  546. context->loginuid = current->audit_context->loginuid;
  547. tsk->audit_context = context;
  548. set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
  549. return 0;
  550. }
  551. static inline void audit_free_context(struct audit_context *context)
  552. {
  553. struct audit_context *previous;
  554. int count = 0;
  555. do {
  556. previous = context->previous;
  557. if (previous || (count && count < 10)) {
  558. ++count;
  559. printk(KERN_ERR "audit(:%d): major=%d name_count=%d:"
  560. " freeing multiple contexts (%d)\n",
  561. context->serial, context->major,
  562. context->name_count, count);
  563. }
  564. audit_free_names(context);
  565. audit_free_aux(context);
  566. kfree(context);
  567. context = previous;
  568. } while (context);
  569. if (count >= 10)
  570. printk(KERN_ERR "audit: freed %d contexts\n", count);
  571. }
  572. static void audit_log_task_context(struct audit_buffer *ab)
  573. {
  574. char *ctx = NULL;
  575. ssize_t len = 0;
  576. len = security_getprocattr(current, "current", NULL, 0);
  577. if (len < 0) {
  578. if (len != -EINVAL)
  579. goto error_path;
  580. return;
  581. }
  582. ctx = kmalloc(len, GFP_KERNEL);
  583. if (!ctx)
  584. goto error_path;
  585. len = security_getprocattr(current, "current", ctx, len);
  586. if (len < 0 )
  587. goto error_path;
  588. audit_log_format(ab, " subj=%s", ctx);
  589. return;
  590. error_path:
  591. if (ctx)
  592. kfree(ctx);
  593. audit_panic("error in audit_log_task_context");
  594. return;
  595. }
  596. static void audit_log_task_info(struct audit_buffer *ab, struct task_struct *tsk)
  597. {
  598. char name[sizeof(tsk->comm)];
  599. struct mm_struct *mm = tsk->mm;
  600. struct vm_area_struct *vma;
  601. /* tsk == current */
  602. get_task_comm(name, tsk);
  603. audit_log_format(ab, " comm=");
  604. audit_log_untrustedstring(ab, name);
  605. if (mm) {
  606. down_read(&mm->mmap_sem);
  607. vma = mm->mmap;
  608. while (vma) {
  609. if ((vma->vm_flags & VM_EXECUTABLE) &&
  610. vma->vm_file) {
  611. audit_log_d_path(ab, "exe=",
  612. vma->vm_file->f_dentry,
  613. vma->vm_file->f_vfsmnt);
  614. break;
  615. }
  616. vma = vma->vm_next;
  617. }
  618. up_read(&mm->mmap_sem);
  619. }
  620. audit_log_task_context(ab);
  621. }
  622. static void audit_log_exit(struct audit_context *context, struct task_struct *tsk)
  623. {
  624. int i, call_panic = 0;
  625. struct audit_buffer *ab;
  626. struct audit_aux_data *aux;
  627. const char *tty;
  628. /* tsk == current */
  629. ab = audit_log_start(context, GFP_KERNEL, AUDIT_SYSCALL);
  630. if (!ab)
  631. return; /* audit_panic has been called */
  632. audit_log_format(ab, "arch=%x syscall=%d",
  633. context->arch, context->major);
  634. if (context->personality != PER_LINUX)
  635. audit_log_format(ab, " per=%lx", context->personality);
  636. if (context->return_valid)
  637. audit_log_format(ab, " success=%s exit=%ld",
  638. (context->return_valid==AUDITSC_SUCCESS)?"yes":"no",
  639. context->return_code);
  640. if (tsk->signal && tsk->signal->tty && tsk->signal->tty->name)
  641. tty = tsk->signal->tty->name;
  642. else
  643. tty = "(none)";
  644. audit_log_format(ab,
  645. " a0=%lx a1=%lx a2=%lx a3=%lx items=%d"
  646. " ppid=%d pid=%d auid=%u uid=%u gid=%u"
  647. " euid=%u suid=%u fsuid=%u"
  648. " egid=%u sgid=%u fsgid=%u tty=%s",
  649. context->argv[0],
  650. context->argv[1],
  651. context->argv[2],
  652. context->argv[3],
  653. context->name_count,
  654. context->ppid,
  655. context->pid,
  656. context->loginuid,
  657. context->uid,
  658. context->gid,
  659. context->euid, context->suid, context->fsuid,
  660. context->egid, context->sgid, context->fsgid, tty);
  661. audit_log_task_info(ab, tsk);
  662. audit_log_end(ab);
  663. for (aux = context->aux; aux; aux = aux->next) {
  664. ab = audit_log_start(context, GFP_KERNEL, aux->type);
  665. if (!ab)
  666. continue; /* audit_panic has been called */
  667. switch (aux->type) {
  668. case AUDIT_MQ_OPEN: {
  669. struct audit_aux_data_mq_open *axi = (void *)aux;
  670. audit_log_format(ab,
  671. "oflag=0x%x mode=%#o mq_flags=0x%lx mq_maxmsg=%ld "
  672. "mq_msgsize=%ld mq_curmsgs=%ld",
  673. axi->oflag, axi->mode, axi->attr.mq_flags,
  674. axi->attr.mq_maxmsg, axi->attr.mq_msgsize,
  675. axi->attr.mq_curmsgs);
  676. break; }
  677. case AUDIT_MQ_SENDRECV: {
  678. struct audit_aux_data_mq_sendrecv *axi = (void *)aux;
  679. audit_log_format(ab,
  680. "mqdes=%d msg_len=%zd msg_prio=%u "
  681. "abs_timeout_sec=%ld abs_timeout_nsec=%ld",
  682. axi->mqdes, axi->msg_len, axi->msg_prio,
  683. axi->abs_timeout.tv_sec, axi->abs_timeout.tv_nsec);
  684. break; }
  685. case AUDIT_MQ_NOTIFY: {
  686. struct audit_aux_data_mq_notify *axi = (void *)aux;
  687. audit_log_format(ab,
  688. "mqdes=%d sigev_signo=%d",
  689. axi->mqdes,
  690. axi->notification.sigev_signo);
  691. break; }
  692. case AUDIT_MQ_GETSETATTR: {
  693. struct audit_aux_data_mq_getsetattr *axi = (void *)aux;
  694. audit_log_format(ab,
  695. "mqdes=%d mq_flags=0x%lx mq_maxmsg=%ld mq_msgsize=%ld "
  696. "mq_curmsgs=%ld ",
  697. axi->mqdes,
  698. axi->mqstat.mq_flags, axi->mqstat.mq_maxmsg,
  699. axi->mqstat.mq_msgsize, axi->mqstat.mq_curmsgs);
  700. break; }
  701. case AUDIT_IPC: {
  702. struct audit_aux_data_ipcctl *axi = (void *)aux;
  703. audit_log_format(ab,
  704. "ouid=%u ogid=%u mode=%x",
  705. axi->uid, axi->gid, axi->mode);
  706. if (axi->osid != 0) {
  707. char *ctx = NULL;
  708. u32 len;
  709. if (selinux_ctxid_to_string(
  710. axi->osid, &ctx, &len)) {
  711. audit_log_format(ab, " osid=%u",
  712. axi->osid);
  713. call_panic = 1;
  714. } else
  715. audit_log_format(ab, " obj=%s", ctx);
  716. kfree(ctx);
  717. }
  718. break; }
  719. case AUDIT_IPC_SET_PERM: {
  720. struct audit_aux_data_ipcctl *axi = (void *)aux;
  721. audit_log_format(ab,
  722. "qbytes=%lx ouid=%u ogid=%u mode=%x",
  723. axi->qbytes, axi->uid, axi->gid, axi->mode);
  724. break; }
  725. case AUDIT_EXECVE: {
  726. struct audit_aux_data_execve *axi = (void *)aux;
  727. int i;
  728. const char *p;
  729. for (i = 0, p = axi->mem; i < axi->argc; i++) {
  730. audit_log_format(ab, "a%d=", i);
  731. p = audit_log_untrustedstring(ab, p);
  732. audit_log_format(ab, "\n");
  733. }
  734. break; }
  735. case AUDIT_SOCKETCALL: {
  736. int i;
  737. struct audit_aux_data_socketcall *axs = (void *)aux;
  738. audit_log_format(ab, "nargs=%d", axs->nargs);
  739. for (i=0; i<axs->nargs; i++)
  740. audit_log_format(ab, " a%d=%lx", i, axs->args[i]);
  741. break; }
  742. case AUDIT_SOCKADDR: {
  743. struct audit_aux_data_sockaddr *axs = (void *)aux;
  744. audit_log_format(ab, "saddr=");
  745. audit_log_hex(ab, axs->a, axs->len);
  746. break; }
  747. case AUDIT_AVC_PATH: {
  748. struct audit_aux_data_path *axi = (void *)aux;
  749. audit_log_d_path(ab, "path=", axi->dentry, axi->mnt);
  750. break; }
  751. }
  752. audit_log_end(ab);
  753. }
  754. if (context->pwd && context->pwdmnt) {
  755. ab = audit_log_start(context, GFP_KERNEL, AUDIT_CWD);
  756. if (ab) {
  757. audit_log_d_path(ab, "cwd=", context->pwd, context->pwdmnt);
  758. audit_log_end(ab);
  759. }
  760. }
  761. for (i = 0; i < context->name_count; i++) {
  762. struct audit_names *n = &context->names[i];
  763. ab = audit_log_start(context, GFP_KERNEL, AUDIT_PATH);
  764. if (!ab)
  765. continue; /* audit_panic has been called */
  766. audit_log_format(ab, "item=%d", i);
  767. if (n->name) {
  768. switch(n->name_len) {
  769. case AUDIT_NAME_FULL:
  770. /* log the full path */
  771. audit_log_format(ab, " name=");
  772. audit_log_untrustedstring(ab, n->name);
  773. break;
  774. case 0:
  775. /* name was specified as a relative path and the
  776. * directory component is the cwd */
  777. audit_log_d_path(ab, " name=", context->pwd,
  778. context->pwdmnt);
  779. break;
  780. default:
  781. /* log the name's directory component */
  782. audit_log_format(ab, " name=");
  783. audit_log_n_untrustedstring(ab, n->name_len,
  784. n->name);
  785. }
  786. } else
  787. audit_log_format(ab, " name=(null)");
  788. if (n->ino != (unsigned long)-1) {
  789. audit_log_format(ab, " inode=%lu"
  790. " dev=%02x:%02x mode=%#o"
  791. " ouid=%u ogid=%u rdev=%02x:%02x",
  792. n->ino,
  793. MAJOR(n->dev),
  794. MINOR(n->dev),
  795. n->mode,
  796. n->uid,
  797. n->gid,
  798. MAJOR(n->rdev),
  799. MINOR(n->rdev));
  800. }
  801. if (n->osid != 0) {
  802. char *ctx = NULL;
  803. u32 len;
  804. if (selinux_ctxid_to_string(
  805. n->osid, &ctx, &len)) {
  806. audit_log_format(ab, " osid=%u", n->osid);
  807. call_panic = 2;
  808. } else
  809. audit_log_format(ab, " obj=%s", ctx);
  810. kfree(ctx);
  811. }
  812. audit_log_end(ab);
  813. }
  814. if (call_panic)
  815. audit_panic("error converting sid to string");
  816. }
  817. /**
  818. * audit_free - free a per-task audit context
  819. * @tsk: task whose audit context block to free
  820. *
  821. * Called from copy_process and do_exit
  822. */
  823. void audit_free(struct task_struct *tsk)
  824. {
  825. struct audit_context *context;
  826. context = audit_get_context(tsk, 0, 0);
  827. if (likely(!context))
  828. return;
  829. /* Check for system calls that do not go through the exit
  830. * function (e.g., exit_group), then free context block.
  831. * We use GFP_ATOMIC here because we might be doing this
  832. * in the context of the idle thread */
  833. /* that can happen only if we are called from do_exit() */
  834. if (context->in_syscall && context->auditable)
  835. audit_log_exit(context, tsk);
  836. audit_free_context(context);
  837. }
  838. /**
  839. * audit_syscall_entry - fill in an audit record at syscall entry
  840. * @tsk: task being audited
  841. * @arch: architecture type
  842. * @major: major syscall type (function)
  843. * @a1: additional syscall register 1
  844. * @a2: additional syscall register 2
  845. * @a3: additional syscall register 3
  846. * @a4: additional syscall register 4
  847. *
  848. * Fill in audit context at syscall entry. This only happens if the
  849. * audit context was created when the task was created and the state or
  850. * filters demand the audit context be built. If the state from the
  851. * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT,
  852. * then the record will be written at syscall exit time (otherwise, it
  853. * will only be written if another part of the kernel requests that it
  854. * be written).
  855. */
  856. void audit_syscall_entry(int arch, int major,
  857. unsigned long a1, unsigned long a2,
  858. unsigned long a3, unsigned long a4)
  859. {
  860. struct task_struct *tsk = current;
  861. struct audit_context *context = tsk->audit_context;
  862. enum audit_state state;
  863. BUG_ON(!context);
  864. /*
  865. * This happens only on certain architectures that make system
  866. * calls in kernel_thread via the entry.S interface, instead of
  867. * with direct calls. (If you are porting to a new
  868. * architecture, hitting this condition can indicate that you
  869. * got the _exit/_leave calls backward in entry.S.)
  870. *
  871. * i386 no
  872. * x86_64 no
  873. * ppc64 yes (see arch/powerpc/platforms/iseries/misc.S)
  874. *
  875. * This also happens with vm86 emulation in a non-nested manner
  876. * (entries without exits), so this case must be caught.
  877. */
  878. if (context->in_syscall) {
  879. struct audit_context *newctx;
  880. #if AUDIT_DEBUG
  881. printk(KERN_ERR
  882. "audit(:%d) pid=%d in syscall=%d;"
  883. " entering syscall=%d\n",
  884. context->serial, tsk->pid, context->major, major);
  885. #endif
  886. newctx = audit_alloc_context(context->state);
  887. if (newctx) {
  888. newctx->previous = context;
  889. context = newctx;
  890. tsk->audit_context = newctx;
  891. } else {
  892. /* If we can't alloc a new context, the best we
  893. * can do is to leak memory (any pending putname
  894. * will be lost). The only other alternative is
  895. * to abandon auditing. */
  896. audit_zero_context(context, context->state);
  897. }
  898. }
  899. BUG_ON(context->in_syscall || context->name_count);
  900. if (!audit_enabled)
  901. return;
  902. context->arch = arch;
  903. context->major = major;
  904. context->argv[0] = a1;
  905. context->argv[1] = a2;
  906. context->argv[2] = a3;
  907. context->argv[3] = a4;
  908. state = context->state;
  909. if (state == AUDIT_SETUP_CONTEXT || state == AUDIT_BUILD_CONTEXT)
  910. state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_ENTRY]);
  911. if (likely(state == AUDIT_DISABLED))
  912. return;
  913. context->serial = 0;
  914. context->ctime = CURRENT_TIME;
  915. context->in_syscall = 1;
  916. context->auditable = !!(state == AUDIT_RECORD_CONTEXT);
  917. }
  918. /**
  919. * audit_syscall_exit - deallocate audit context after a system call
  920. * @tsk: task being audited
  921. * @valid: success/failure flag
  922. * @return_code: syscall return value
  923. *
  924. * Tear down after system call. If the audit context has been marked as
  925. * auditable (either because of the AUDIT_RECORD_CONTEXT state from
  926. * filtering, or because some other part of the kernel write an audit
  927. * message), then write out the syscall information. In call cases,
  928. * free the names stored from getname().
  929. */
  930. void audit_syscall_exit(int valid, long return_code)
  931. {
  932. struct task_struct *tsk = current;
  933. struct audit_context *context;
  934. context = audit_get_context(tsk, valid, return_code);
  935. if (likely(!context))
  936. return;
  937. if (context->in_syscall && context->auditable)
  938. audit_log_exit(context, tsk);
  939. context->in_syscall = 0;
  940. context->auditable = 0;
  941. if (context->previous) {
  942. struct audit_context *new_context = context->previous;
  943. context->previous = NULL;
  944. audit_free_context(context);
  945. tsk->audit_context = new_context;
  946. } else {
  947. audit_free_names(context);
  948. audit_free_aux(context);
  949. tsk->audit_context = context;
  950. }
  951. }
  952. /**
  953. * audit_getname - add a name to the list
  954. * @name: name to add
  955. *
  956. * Add a name to the list of audit names for this context.
  957. * Called from fs/namei.c:getname().
  958. */
  959. void __audit_getname(const char *name)
  960. {
  961. struct audit_context *context = current->audit_context;
  962. if (IS_ERR(name) || !name)
  963. return;
  964. if (!context->in_syscall) {
  965. #if AUDIT_DEBUG == 2
  966. printk(KERN_ERR "%s:%d(:%d): ignoring getname(%p)\n",
  967. __FILE__, __LINE__, context->serial, name);
  968. dump_stack();
  969. #endif
  970. return;
  971. }
  972. BUG_ON(context->name_count >= AUDIT_NAMES);
  973. context->names[context->name_count].name = name;
  974. context->names[context->name_count].name_len = AUDIT_NAME_FULL;
  975. context->names[context->name_count].name_put = 1;
  976. context->names[context->name_count].ino = (unsigned long)-1;
  977. ++context->name_count;
  978. if (!context->pwd) {
  979. read_lock(&current->fs->lock);
  980. context->pwd = dget(current->fs->pwd);
  981. context->pwdmnt = mntget(current->fs->pwdmnt);
  982. read_unlock(&current->fs->lock);
  983. }
  984. }
  985. /* audit_putname - intercept a putname request
  986. * @name: name to intercept and delay for putname
  987. *
  988. * If we have stored the name from getname in the audit context,
  989. * then we delay the putname until syscall exit.
  990. * Called from include/linux/fs.h:putname().
  991. */
  992. void audit_putname(const char *name)
  993. {
  994. struct audit_context *context = current->audit_context;
  995. BUG_ON(!context);
  996. if (!context->in_syscall) {
  997. #if AUDIT_DEBUG == 2
  998. printk(KERN_ERR "%s:%d(:%d): __putname(%p)\n",
  999. __FILE__, __LINE__, context->serial, name);
  1000. if (context->name_count) {
  1001. int i;
  1002. for (i = 0; i < context->name_count; i++)
  1003. printk(KERN_ERR "name[%d] = %p = %s\n", i,
  1004. context->names[i].name,
  1005. context->names[i].name ?: "(null)");
  1006. }
  1007. #endif
  1008. __putname(name);
  1009. }
  1010. #if AUDIT_DEBUG
  1011. else {
  1012. ++context->put_count;
  1013. if (context->put_count > context->name_count) {
  1014. printk(KERN_ERR "%s:%d(:%d): major=%d"
  1015. " in_syscall=%d putname(%p) name_count=%d"
  1016. " put_count=%d\n",
  1017. __FILE__, __LINE__,
  1018. context->serial, context->major,
  1019. context->in_syscall, name, context->name_count,
  1020. context->put_count);
  1021. dump_stack();
  1022. }
  1023. }
  1024. #endif
  1025. }
  1026. static void audit_inode_context(int idx, const struct inode *inode)
  1027. {
  1028. struct audit_context *context = current->audit_context;
  1029. selinux_get_inode_sid(inode, &context->names[idx].osid);
  1030. }
  1031. /**
  1032. * audit_inode - store the inode and device from a lookup
  1033. * @name: name being audited
  1034. * @inode: inode being audited
  1035. *
  1036. * Called from fs/namei.c:path_lookup().
  1037. */
  1038. void __audit_inode(const char *name, const struct inode *inode)
  1039. {
  1040. int idx;
  1041. struct audit_context *context = current->audit_context;
  1042. if (!context->in_syscall)
  1043. return;
  1044. if (context->name_count
  1045. && context->names[context->name_count-1].name
  1046. && context->names[context->name_count-1].name == name)
  1047. idx = context->name_count - 1;
  1048. else if (context->name_count > 1
  1049. && context->names[context->name_count-2].name
  1050. && context->names[context->name_count-2].name == name)
  1051. idx = context->name_count - 2;
  1052. else {
  1053. /* FIXME: how much do we care about inodes that have no
  1054. * associated name? */
  1055. if (context->name_count >= AUDIT_NAMES - AUDIT_NAMES_RESERVED)
  1056. return;
  1057. idx = context->name_count++;
  1058. context->names[idx].name = NULL;
  1059. #if AUDIT_DEBUG
  1060. ++context->ino_count;
  1061. #endif
  1062. }
  1063. context->names[idx].ino = inode->i_ino;
  1064. context->names[idx].dev = inode->i_sb->s_dev;
  1065. context->names[idx].mode = inode->i_mode;
  1066. context->names[idx].uid = inode->i_uid;
  1067. context->names[idx].gid = inode->i_gid;
  1068. context->names[idx].rdev = inode->i_rdev;
  1069. audit_inode_context(idx, inode);
  1070. }
  1071. /**
  1072. * audit_inode_child - collect inode info for created/removed objects
  1073. * @dname: inode's dentry name
  1074. * @inode: inode being audited
  1075. * @pino: inode number of dentry parent
  1076. *
  1077. * For syscalls that create or remove filesystem objects, audit_inode
  1078. * can only collect information for the filesystem object's parent.
  1079. * This call updates the audit context with the child's information.
  1080. * Syscalls that create a new filesystem object must be hooked after
  1081. * the object is created. Syscalls that remove a filesystem object
  1082. * must be hooked prior, in order to capture the target inode during
  1083. * unsuccessful attempts.
  1084. */
  1085. void __audit_inode_child(const char *dname, const struct inode *inode,
  1086. unsigned long pino)
  1087. {
  1088. int idx;
  1089. struct audit_context *context = current->audit_context;
  1090. const char *found_name = NULL;
  1091. int dirlen = 0;
  1092. if (!context->in_syscall)
  1093. return;
  1094. /* determine matching parent */
  1095. if (!dname)
  1096. goto update_context;
  1097. for (idx = 0; idx < context->name_count; idx++)
  1098. if (context->names[idx].ino == pino) {
  1099. const char *name = context->names[idx].name;
  1100. if (!name)
  1101. continue;
  1102. if (audit_compare_dname_path(dname, name, &dirlen) == 0) {
  1103. context->names[idx].name_len = dirlen;
  1104. found_name = name;
  1105. break;
  1106. }
  1107. }
  1108. update_context:
  1109. idx = context->name_count++;
  1110. #if AUDIT_DEBUG
  1111. context->ino_count++;
  1112. #endif
  1113. /* Re-use the name belonging to the slot for a matching parent directory.
  1114. * All names for this context are relinquished in audit_free_names() */
  1115. context->names[idx].name = found_name;
  1116. context->names[idx].name_len = AUDIT_NAME_FULL;
  1117. context->names[idx].name_put = 0; /* don't call __putname() */
  1118. if (inode) {
  1119. context->names[idx].ino = inode->i_ino;
  1120. context->names[idx].dev = inode->i_sb->s_dev;
  1121. context->names[idx].mode = inode->i_mode;
  1122. context->names[idx].uid = inode->i_uid;
  1123. context->names[idx].gid = inode->i_gid;
  1124. context->names[idx].rdev = inode->i_rdev;
  1125. audit_inode_context(idx, inode);
  1126. } else
  1127. context->names[idx].ino = (unsigned long)-1;
  1128. }
  1129. /**
  1130. * auditsc_get_stamp - get local copies of audit_context values
  1131. * @ctx: audit_context for the task
  1132. * @t: timespec to store time recorded in the audit_context
  1133. * @serial: serial value that is recorded in the audit_context
  1134. *
  1135. * Also sets the context as auditable.
  1136. */
  1137. void auditsc_get_stamp(struct audit_context *ctx,
  1138. struct timespec *t, unsigned int *serial)
  1139. {
  1140. if (!ctx->serial)
  1141. ctx->serial = audit_serial();
  1142. t->tv_sec = ctx->ctime.tv_sec;
  1143. t->tv_nsec = ctx->ctime.tv_nsec;
  1144. *serial = ctx->serial;
  1145. ctx->auditable = 1;
  1146. }
  1147. /**
  1148. * audit_set_loginuid - set a task's audit_context loginuid
  1149. * @task: task whose audit context is being modified
  1150. * @loginuid: loginuid value
  1151. *
  1152. * Returns 0.
  1153. *
  1154. * Called (set) from fs/proc/base.c::proc_loginuid_write().
  1155. */
  1156. int audit_set_loginuid(struct task_struct *task, uid_t loginuid)
  1157. {
  1158. struct audit_context *context = task->audit_context;
  1159. if (context) {
  1160. /* Only log if audit is enabled */
  1161. if (context->in_syscall) {
  1162. struct audit_buffer *ab;
  1163. ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_LOGIN);
  1164. if (ab) {
  1165. audit_log_format(ab, "login pid=%d uid=%u "
  1166. "old auid=%u new auid=%u",
  1167. task->pid, task->uid,
  1168. context->loginuid, loginuid);
  1169. audit_log_end(ab);
  1170. }
  1171. }
  1172. context->loginuid = loginuid;
  1173. }
  1174. return 0;
  1175. }
  1176. /**
  1177. * audit_get_loginuid - get the loginuid for an audit_context
  1178. * @ctx: the audit_context
  1179. *
  1180. * Returns the context's loginuid or -1 if @ctx is NULL.
  1181. */
  1182. uid_t audit_get_loginuid(struct audit_context *ctx)
  1183. {
  1184. return ctx ? ctx->loginuid : -1;
  1185. }
  1186. /**
  1187. * __audit_mq_open - record audit data for a POSIX MQ open
  1188. * @oflag: open flag
  1189. * @mode: mode bits
  1190. * @u_attr: queue attributes
  1191. *
  1192. * Returns 0 for success or NULL context or < 0 on error.
  1193. */
  1194. int __audit_mq_open(int oflag, mode_t mode, struct mq_attr __user *u_attr)
  1195. {
  1196. struct audit_aux_data_mq_open *ax;
  1197. struct audit_context *context = current->audit_context;
  1198. if (!audit_enabled)
  1199. return 0;
  1200. if (likely(!context))
  1201. return 0;
  1202. ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
  1203. if (!ax)
  1204. return -ENOMEM;
  1205. if (u_attr != NULL) {
  1206. if (copy_from_user(&ax->attr, u_attr, sizeof(ax->attr))) {
  1207. kfree(ax);
  1208. return -EFAULT;
  1209. }
  1210. } else
  1211. memset(&ax->attr, 0, sizeof(ax->attr));
  1212. ax->oflag = oflag;
  1213. ax->mode = mode;
  1214. ax->d.type = AUDIT_MQ_OPEN;
  1215. ax->d.next = context->aux;
  1216. context->aux = (void *)ax;
  1217. return 0;
  1218. }
  1219. /**
  1220. * __audit_mq_timedsend - record audit data for a POSIX MQ timed send
  1221. * @mqdes: MQ descriptor
  1222. * @msg_len: Message length
  1223. * @msg_prio: Message priority
  1224. * @abs_timeout: Message timeout in absolute time
  1225. *
  1226. * Returns 0 for success or NULL context or < 0 on error.
  1227. */
  1228. int __audit_mq_timedsend(mqd_t mqdes, size_t msg_len, unsigned int msg_prio,
  1229. const struct timespec __user *u_abs_timeout)
  1230. {
  1231. struct audit_aux_data_mq_sendrecv *ax;
  1232. struct audit_context *context = current->audit_context;
  1233. if (!audit_enabled)
  1234. return 0;
  1235. if (likely(!context))
  1236. return 0;
  1237. ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
  1238. if (!ax)
  1239. return -ENOMEM;
  1240. if (u_abs_timeout != NULL) {
  1241. if (copy_from_user(&ax->abs_timeout, u_abs_timeout, sizeof(ax->abs_timeout))) {
  1242. kfree(ax);
  1243. return -EFAULT;
  1244. }
  1245. } else
  1246. memset(&ax->abs_timeout, 0, sizeof(ax->abs_timeout));
  1247. ax->mqdes = mqdes;
  1248. ax->msg_len = msg_len;
  1249. ax->msg_prio = msg_prio;
  1250. ax->d.type = AUDIT_MQ_SENDRECV;
  1251. ax->d.next = context->aux;
  1252. context->aux = (void *)ax;
  1253. return 0;
  1254. }
  1255. /**
  1256. * __audit_mq_timedreceive - record audit data for a POSIX MQ timed receive
  1257. * @mqdes: MQ descriptor
  1258. * @msg_len: Message length
  1259. * @msg_prio: Message priority
  1260. * @abs_timeout: Message timeout in absolute time
  1261. *
  1262. * Returns 0 for success or NULL context or < 0 on error.
  1263. */
  1264. int __audit_mq_timedreceive(mqd_t mqdes, size_t msg_len,
  1265. unsigned int __user *u_msg_prio,
  1266. const struct timespec __user *u_abs_timeout)
  1267. {
  1268. struct audit_aux_data_mq_sendrecv *ax;
  1269. struct audit_context *context = current->audit_context;
  1270. if (!audit_enabled)
  1271. return 0;
  1272. if (likely(!context))
  1273. return 0;
  1274. ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
  1275. if (!ax)
  1276. return -ENOMEM;
  1277. if (u_msg_prio != NULL) {
  1278. if (get_user(ax->msg_prio, u_msg_prio)) {
  1279. kfree(ax);
  1280. return -EFAULT;
  1281. }
  1282. } else
  1283. ax->msg_prio = 0;
  1284. if (u_abs_timeout != NULL) {
  1285. if (copy_from_user(&ax->abs_timeout, u_abs_timeout, sizeof(ax->abs_timeout))) {
  1286. kfree(ax);
  1287. return -EFAULT;
  1288. }
  1289. } else
  1290. memset(&ax->abs_timeout, 0, sizeof(ax->abs_timeout));
  1291. ax->mqdes = mqdes;
  1292. ax->msg_len = msg_len;
  1293. ax->d.type = AUDIT_MQ_SENDRECV;
  1294. ax->d.next = context->aux;
  1295. context->aux = (void *)ax;
  1296. return 0;
  1297. }
  1298. /**
  1299. * __audit_mq_notify - record audit data for a POSIX MQ notify
  1300. * @mqdes: MQ descriptor
  1301. * @u_notification: Notification event
  1302. *
  1303. * Returns 0 for success or NULL context or < 0 on error.
  1304. */
  1305. int __audit_mq_notify(mqd_t mqdes, const struct sigevent __user *u_notification)
  1306. {
  1307. struct audit_aux_data_mq_notify *ax;
  1308. struct audit_context *context = current->audit_context;
  1309. if (!audit_enabled)
  1310. return 0;
  1311. if (likely(!context))
  1312. return 0;
  1313. ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
  1314. if (!ax)
  1315. return -ENOMEM;
  1316. if (u_notification != NULL) {
  1317. if (copy_from_user(&ax->notification, u_notification, sizeof(ax->notification))) {
  1318. kfree(ax);
  1319. return -EFAULT;
  1320. }
  1321. } else
  1322. memset(&ax->notification, 0, sizeof(ax->notification));
  1323. ax->mqdes = mqdes;
  1324. ax->d.type = AUDIT_MQ_NOTIFY;
  1325. ax->d.next = context->aux;
  1326. context->aux = (void *)ax;
  1327. return 0;
  1328. }
  1329. /**
  1330. * __audit_mq_getsetattr - record audit data for a POSIX MQ get/set attribute
  1331. * @mqdes: MQ descriptor
  1332. * @mqstat: MQ flags
  1333. *
  1334. * Returns 0 for success or NULL context or < 0 on error.
  1335. */
  1336. int __audit_mq_getsetattr(mqd_t mqdes, struct mq_attr *mqstat)
  1337. {
  1338. struct audit_aux_data_mq_getsetattr *ax;
  1339. struct audit_context *context = current->audit_context;
  1340. if (!audit_enabled)
  1341. return 0;
  1342. if (likely(!context))
  1343. return 0;
  1344. ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
  1345. if (!ax)
  1346. return -ENOMEM;
  1347. ax->mqdes = mqdes;
  1348. ax->mqstat = *mqstat;
  1349. ax->d.type = AUDIT_MQ_GETSETATTR;
  1350. ax->d.next = context->aux;
  1351. context->aux = (void *)ax;
  1352. return 0;
  1353. }
  1354. /**
  1355. * audit_ipc_obj - record audit data for ipc object
  1356. * @ipcp: ipc permissions
  1357. *
  1358. * Returns 0 for success or NULL context or < 0 on error.
  1359. */
  1360. int __audit_ipc_obj(struct kern_ipc_perm *ipcp)
  1361. {
  1362. struct audit_aux_data_ipcctl *ax;
  1363. struct audit_context *context = current->audit_context;
  1364. ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
  1365. if (!ax)
  1366. return -ENOMEM;
  1367. ax->uid = ipcp->uid;
  1368. ax->gid = ipcp->gid;
  1369. ax->mode = ipcp->mode;
  1370. selinux_get_ipc_sid(ipcp, &ax->osid);
  1371. ax->d.type = AUDIT_IPC;
  1372. ax->d.next = context->aux;
  1373. context->aux = (void *)ax;
  1374. return 0;
  1375. }
  1376. /**
  1377. * audit_ipc_set_perm - record audit data for new ipc permissions
  1378. * @qbytes: msgq bytes
  1379. * @uid: msgq user id
  1380. * @gid: msgq group id
  1381. * @mode: msgq mode (permissions)
  1382. *
  1383. * Returns 0 for success or NULL context or < 0 on error.
  1384. */
  1385. int __audit_ipc_set_perm(unsigned long qbytes, uid_t uid, gid_t gid, mode_t mode)
  1386. {
  1387. struct audit_aux_data_ipcctl *ax;
  1388. struct audit_context *context = current->audit_context;
  1389. ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
  1390. if (!ax)
  1391. return -ENOMEM;
  1392. ax->qbytes = qbytes;
  1393. ax->uid = uid;
  1394. ax->gid = gid;
  1395. ax->mode = mode;
  1396. ax->d.type = AUDIT_IPC_SET_PERM;
  1397. ax->d.next = context->aux;
  1398. context->aux = (void *)ax;
  1399. return 0;
  1400. }
  1401. int audit_bprm(struct linux_binprm *bprm)
  1402. {
  1403. struct audit_aux_data_execve *ax;
  1404. struct audit_context *context = current->audit_context;
  1405. unsigned long p, next;
  1406. void *to;
  1407. if (likely(!audit_enabled || !context))
  1408. return 0;
  1409. ax = kmalloc(sizeof(*ax) + PAGE_SIZE * MAX_ARG_PAGES - bprm->p,
  1410. GFP_KERNEL);
  1411. if (!ax)
  1412. return -ENOMEM;
  1413. ax->argc = bprm->argc;
  1414. ax->envc = bprm->envc;
  1415. for (p = bprm->p, to = ax->mem; p < MAX_ARG_PAGES*PAGE_SIZE; p = next) {
  1416. struct page *page = bprm->page[p / PAGE_SIZE];
  1417. void *kaddr = kmap(page);
  1418. next = (p + PAGE_SIZE) & ~(PAGE_SIZE - 1);
  1419. memcpy(to, kaddr + (p & (PAGE_SIZE - 1)), next - p);
  1420. to += next - p;
  1421. kunmap(page);
  1422. }
  1423. ax->d.type = AUDIT_EXECVE;
  1424. ax->d.next = context->aux;
  1425. context->aux = (void *)ax;
  1426. return 0;
  1427. }
  1428. /**
  1429. * audit_socketcall - record audit data for sys_socketcall
  1430. * @nargs: number of args
  1431. * @args: args array
  1432. *
  1433. * Returns 0 for success or NULL context or < 0 on error.
  1434. */
  1435. int audit_socketcall(int nargs, unsigned long *args)
  1436. {
  1437. struct audit_aux_data_socketcall *ax;
  1438. struct audit_context *context = current->audit_context;
  1439. if (likely(!context))
  1440. return 0;
  1441. ax = kmalloc(sizeof(*ax) + nargs * sizeof(unsigned long), GFP_KERNEL);
  1442. if (!ax)
  1443. return -ENOMEM;
  1444. ax->nargs = nargs;
  1445. memcpy(ax->args, args, nargs * sizeof(unsigned long));
  1446. ax->d.type = AUDIT_SOCKETCALL;
  1447. ax->d.next = context->aux;
  1448. context->aux = (void *)ax;
  1449. return 0;
  1450. }
  1451. /**
  1452. * audit_sockaddr - record audit data for sys_bind, sys_connect, sys_sendto
  1453. * @len: data length in user space
  1454. * @a: data address in kernel space
  1455. *
  1456. * Returns 0 for success or NULL context or < 0 on error.
  1457. */
  1458. int audit_sockaddr(int len, void *a)
  1459. {
  1460. struct audit_aux_data_sockaddr *ax;
  1461. struct audit_context *context = current->audit_context;
  1462. if (likely(!context))
  1463. return 0;
  1464. ax = kmalloc(sizeof(*ax) + len, GFP_KERNEL);
  1465. if (!ax)
  1466. return -ENOMEM;
  1467. ax->len = len;
  1468. memcpy(ax->a, a, len);
  1469. ax->d.type = AUDIT_SOCKADDR;
  1470. ax->d.next = context->aux;
  1471. context->aux = (void *)ax;
  1472. return 0;
  1473. }
  1474. /**
  1475. * audit_avc_path - record the granting or denial of permissions
  1476. * @dentry: dentry to record
  1477. * @mnt: mnt to record
  1478. *
  1479. * Returns 0 for success or NULL context or < 0 on error.
  1480. *
  1481. * Called from security/selinux/avc.c::avc_audit()
  1482. */
  1483. int audit_avc_path(struct dentry *dentry, struct vfsmount *mnt)
  1484. {
  1485. struct audit_aux_data_path *ax;
  1486. struct audit_context *context = current->audit_context;
  1487. if (likely(!context))
  1488. return 0;
  1489. ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
  1490. if (!ax)
  1491. return -ENOMEM;
  1492. ax->dentry = dget(dentry);
  1493. ax->mnt = mntget(mnt);
  1494. ax->d.type = AUDIT_AVC_PATH;
  1495. ax->d.next = context->aux;
  1496. context->aux = (void *)ax;
  1497. return 0;
  1498. }
  1499. /**
  1500. * audit_signal_info - record signal info for shutting down audit subsystem
  1501. * @sig: signal value
  1502. * @t: task being signaled
  1503. *
  1504. * If the audit subsystem is being terminated, record the task (pid)
  1505. * and uid that is doing that.
  1506. */
  1507. void __audit_signal_info(int sig, struct task_struct *t)
  1508. {
  1509. extern pid_t audit_sig_pid;
  1510. extern uid_t audit_sig_uid;
  1511. extern u32 audit_sig_sid;
  1512. if (sig == SIGTERM || sig == SIGHUP || sig == SIGUSR1) {
  1513. struct task_struct *tsk = current;
  1514. struct audit_context *ctx = tsk->audit_context;
  1515. audit_sig_pid = tsk->pid;
  1516. if (ctx)
  1517. audit_sig_uid = ctx->loginuid;
  1518. else
  1519. audit_sig_uid = tsk->uid;
  1520. selinux_get_task_sid(tsk, &audit_sig_sid);
  1521. }
  1522. }