auditsc.c 52 KB

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