auditsc.c 52 KB

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