auditsc.c 52 KB

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