auditsc.c 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543
  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/binfmts.h>
  64. #include <linux/highmem.h>
  65. #include <linux/syscalls.h>
  66. #include <linux/inotify.h>
  67. #include <linux/capability.h>
  68. #include "audit.h"
  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. /* no execve audit message should be longer than this (userspace limits) */
  75. #define MAX_EXECVE_AUDIT_LEN 7500
  76. /* number of audit rules */
  77. int audit_n_rules;
  78. /* determines whether we collect data for signals sent */
  79. int audit_signals;
  80. struct audit_cap_data {
  81. kernel_cap_t permitted;
  82. kernel_cap_t inheritable;
  83. union {
  84. unsigned int fE; /* effective bit of a file capability */
  85. kernel_cap_t effective; /* effective set of a process */
  86. };
  87. };
  88. /* When fs/namei.c:getname() is called, we store the pointer in name and
  89. * we don't let putname() free it (instead we free all of the saved
  90. * pointers at syscall exit time).
  91. *
  92. * Further, in fs/namei.c:path_lookup() we store the inode and device. */
  93. struct audit_names {
  94. const char *name;
  95. int name_len; /* number of name's characters to log */
  96. unsigned name_put; /* call __putname() for this name */
  97. unsigned long ino;
  98. dev_t dev;
  99. umode_t mode;
  100. uid_t uid;
  101. gid_t gid;
  102. dev_t rdev;
  103. u32 osid;
  104. struct audit_cap_data fcap;
  105. unsigned int fcap_ver;
  106. };
  107. struct audit_aux_data {
  108. struct audit_aux_data *next;
  109. int type;
  110. };
  111. #define AUDIT_AUX_IPCPERM 0
  112. /* Number of target pids per aux struct. */
  113. #define AUDIT_AUX_PIDS 16
  114. struct audit_aux_data_mq_open {
  115. struct audit_aux_data d;
  116. int oflag;
  117. mode_t mode;
  118. struct mq_attr attr;
  119. };
  120. struct audit_aux_data_mq_sendrecv {
  121. struct audit_aux_data d;
  122. mqd_t mqdes;
  123. size_t msg_len;
  124. unsigned int msg_prio;
  125. struct timespec abs_timeout;
  126. };
  127. struct audit_aux_data_mq_notify {
  128. struct audit_aux_data d;
  129. mqd_t mqdes;
  130. struct sigevent notification;
  131. };
  132. struct audit_aux_data_mq_getsetattr {
  133. struct audit_aux_data d;
  134. mqd_t mqdes;
  135. struct mq_attr mqstat;
  136. };
  137. struct audit_aux_data_ipcctl {
  138. struct audit_aux_data d;
  139. struct ipc_perm p;
  140. unsigned long qbytes;
  141. uid_t uid;
  142. gid_t gid;
  143. mode_t mode;
  144. u32 osid;
  145. };
  146. struct audit_aux_data_execve {
  147. struct audit_aux_data d;
  148. int argc;
  149. int envc;
  150. struct mm_struct *mm;
  151. };
  152. struct audit_aux_data_socketcall {
  153. struct audit_aux_data d;
  154. int nargs;
  155. unsigned long args[0];
  156. };
  157. struct audit_aux_data_sockaddr {
  158. struct audit_aux_data d;
  159. int len;
  160. char a[0];
  161. };
  162. struct audit_aux_data_fd_pair {
  163. struct audit_aux_data d;
  164. int fd[2];
  165. };
  166. struct audit_aux_data_pids {
  167. struct audit_aux_data d;
  168. pid_t target_pid[AUDIT_AUX_PIDS];
  169. uid_t target_auid[AUDIT_AUX_PIDS];
  170. uid_t target_uid[AUDIT_AUX_PIDS];
  171. unsigned int target_sessionid[AUDIT_AUX_PIDS];
  172. u32 target_sid[AUDIT_AUX_PIDS];
  173. char target_comm[AUDIT_AUX_PIDS][TASK_COMM_LEN];
  174. int pid_count;
  175. };
  176. struct audit_tree_refs {
  177. struct audit_tree_refs *next;
  178. struct audit_chunk *c[31];
  179. };
  180. /* The per-task audit context. */
  181. struct audit_context {
  182. int dummy; /* must be the first element */
  183. int in_syscall; /* 1 if task is in a syscall */
  184. enum audit_state state;
  185. unsigned int serial; /* serial number for record */
  186. struct timespec ctime; /* time of syscall entry */
  187. int major; /* syscall number */
  188. unsigned long argv[4]; /* syscall arguments */
  189. int return_valid; /* return code is valid */
  190. long return_code;/* syscall return code */
  191. int auditable; /* 1 if record should be written */
  192. int name_count;
  193. struct audit_names names[AUDIT_NAMES];
  194. char * filterkey; /* key for rule that triggered record */
  195. struct path pwd;
  196. struct audit_context *previous; /* For nested syscalls */
  197. struct audit_aux_data *aux;
  198. struct audit_aux_data *aux_pids;
  199. /* Save things to print about task_struct */
  200. pid_t pid, ppid;
  201. uid_t uid, euid, suid, fsuid;
  202. gid_t gid, egid, sgid, fsgid;
  203. unsigned long personality;
  204. int arch;
  205. pid_t target_pid;
  206. uid_t target_auid;
  207. uid_t target_uid;
  208. unsigned int target_sessionid;
  209. u32 target_sid;
  210. char target_comm[TASK_COMM_LEN];
  211. struct audit_tree_refs *trees, *first_trees;
  212. int tree_count;
  213. #if AUDIT_DEBUG
  214. int put_count;
  215. int ino_count;
  216. #endif
  217. };
  218. #define ACC_MODE(x) ("\004\002\006\006"[(x)&O_ACCMODE])
  219. static inline int open_arg(int flags, int mask)
  220. {
  221. int n = ACC_MODE(flags);
  222. if (flags & (O_TRUNC | O_CREAT))
  223. n |= AUDIT_PERM_WRITE;
  224. return n & mask;
  225. }
  226. static int audit_match_perm(struct audit_context *ctx, int mask)
  227. {
  228. unsigned n;
  229. if (unlikely(!ctx))
  230. return 0;
  231. n = ctx->major;
  232. switch (audit_classify_syscall(ctx->arch, n)) {
  233. case 0: /* native */
  234. if ((mask & AUDIT_PERM_WRITE) &&
  235. audit_match_class(AUDIT_CLASS_WRITE, n))
  236. return 1;
  237. if ((mask & AUDIT_PERM_READ) &&
  238. audit_match_class(AUDIT_CLASS_READ, n))
  239. return 1;
  240. if ((mask & AUDIT_PERM_ATTR) &&
  241. audit_match_class(AUDIT_CLASS_CHATTR, n))
  242. return 1;
  243. return 0;
  244. case 1: /* 32bit on biarch */
  245. if ((mask & AUDIT_PERM_WRITE) &&
  246. audit_match_class(AUDIT_CLASS_WRITE_32, n))
  247. return 1;
  248. if ((mask & AUDIT_PERM_READ) &&
  249. audit_match_class(AUDIT_CLASS_READ_32, n))
  250. return 1;
  251. if ((mask & AUDIT_PERM_ATTR) &&
  252. audit_match_class(AUDIT_CLASS_CHATTR_32, n))
  253. return 1;
  254. return 0;
  255. case 2: /* open */
  256. return mask & ACC_MODE(ctx->argv[1]);
  257. case 3: /* openat */
  258. return mask & ACC_MODE(ctx->argv[2]);
  259. case 4: /* socketcall */
  260. return ((mask & AUDIT_PERM_WRITE) && ctx->argv[0] == SYS_BIND);
  261. case 5: /* execve */
  262. return mask & AUDIT_PERM_EXEC;
  263. default:
  264. return 0;
  265. }
  266. }
  267. static int audit_match_filetype(struct audit_context *ctx, int which)
  268. {
  269. unsigned index = which & ~S_IFMT;
  270. mode_t mode = which & S_IFMT;
  271. if (unlikely(!ctx))
  272. return 0;
  273. if (index >= ctx->name_count)
  274. return 0;
  275. if (ctx->names[index].ino == -1)
  276. return 0;
  277. if ((ctx->names[index].mode ^ mode) & S_IFMT)
  278. return 0;
  279. return 1;
  280. }
  281. /*
  282. * We keep a linked list of fixed-sized (31 pointer) arrays of audit_chunk *;
  283. * ->first_trees points to its beginning, ->trees - to the current end of data.
  284. * ->tree_count is the number of free entries in array pointed to by ->trees.
  285. * Original condition is (NULL, NULL, 0); as soon as it grows we never revert to NULL,
  286. * "empty" becomes (p, p, 31) afterwards. We don't shrink the list (and seriously,
  287. * it's going to remain 1-element for almost any setup) until we free context itself.
  288. * References in it _are_ dropped - at the same time we free/drop aux stuff.
  289. */
  290. #ifdef CONFIG_AUDIT_TREE
  291. static int put_tree_ref(struct audit_context *ctx, struct audit_chunk *chunk)
  292. {
  293. struct audit_tree_refs *p = ctx->trees;
  294. int left = ctx->tree_count;
  295. if (likely(left)) {
  296. p->c[--left] = chunk;
  297. ctx->tree_count = left;
  298. return 1;
  299. }
  300. if (!p)
  301. return 0;
  302. p = p->next;
  303. if (p) {
  304. p->c[30] = chunk;
  305. ctx->trees = p;
  306. ctx->tree_count = 30;
  307. return 1;
  308. }
  309. return 0;
  310. }
  311. static int grow_tree_refs(struct audit_context *ctx)
  312. {
  313. struct audit_tree_refs *p = ctx->trees;
  314. ctx->trees = kzalloc(sizeof(struct audit_tree_refs), GFP_KERNEL);
  315. if (!ctx->trees) {
  316. ctx->trees = p;
  317. return 0;
  318. }
  319. if (p)
  320. p->next = ctx->trees;
  321. else
  322. ctx->first_trees = ctx->trees;
  323. ctx->tree_count = 31;
  324. return 1;
  325. }
  326. #endif
  327. static void unroll_tree_refs(struct audit_context *ctx,
  328. struct audit_tree_refs *p, int count)
  329. {
  330. #ifdef CONFIG_AUDIT_TREE
  331. struct audit_tree_refs *q;
  332. int n;
  333. if (!p) {
  334. /* we started with empty chain */
  335. p = ctx->first_trees;
  336. count = 31;
  337. /* if the very first allocation has failed, nothing to do */
  338. if (!p)
  339. return;
  340. }
  341. n = count;
  342. for (q = p; q != ctx->trees; q = q->next, n = 31) {
  343. while (n--) {
  344. audit_put_chunk(q->c[n]);
  345. q->c[n] = NULL;
  346. }
  347. }
  348. while (n-- > ctx->tree_count) {
  349. audit_put_chunk(q->c[n]);
  350. q->c[n] = NULL;
  351. }
  352. ctx->trees = p;
  353. ctx->tree_count = count;
  354. #endif
  355. }
  356. static void free_tree_refs(struct audit_context *ctx)
  357. {
  358. struct audit_tree_refs *p, *q;
  359. for (p = ctx->first_trees; p; p = q) {
  360. q = p->next;
  361. kfree(p);
  362. }
  363. }
  364. static int match_tree_refs(struct audit_context *ctx, struct audit_tree *tree)
  365. {
  366. #ifdef CONFIG_AUDIT_TREE
  367. struct audit_tree_refs *p;
  368. int n;
  369. if (!tree)
  370. return 0;
  371. /* full ones */
  372. for (p = ctx->first_trees; p != ctx->trees; p = p->next) {
  373. for (n = 0; n < 31; n++)
  374. if (audit_tree_match(p->c[n], tree))
  375. return 1;
  376. }
  377. /* partial */
  378. if (p) {
  379. for (n = ctx->tree_count; n < 31; n++)
  380. if (audit_tree_match(p->c[n], tree))
  381. return 1;
  382. }
  383. #endif
  384. return 0;
  385. }
  386. /* Determine if any context name data matches a rule's watch data */
  387. /* Compare a task_struct with an audit_rule. Return 1 on match, 0
  388. * otherwise. */
  389. static int audit_filter_rules(struct task_struct *tsk,
  390. struct audit_krule *rule,
  391. struct audit_context *ctx,
  392. struct audit_names *name,
  393. enum audit_state *state)
  394. {
  395. int i, j, need_sid = 1;
  396. u32 sid;
  397. for (i = 0; i < rule->field_count; i++) {
  398. struct audit_field *f = &rule->fields[i];
  399. int result = 0;
  400. switch (f->type) {
  401. case AUDIT_PID:
  402. result = audit_comparator(tsk->pid, f->op, f->val);
  403. break;
  404. case AUDIT_PPID:
  405. if (ctx) {
  406. if (!ctx->ppid)
  407. ctx->ppid = sys_getppid();
  408. result = audit_comparator(ctx->ppid, f->op, f->val);
  409. }
  410. break;
  411. case AUDIT_UID:
  412. result = audit_comparator(tsk->uid, f->op, f->val);
  413. break;
  414. case AUDIT_EUID:
  415. result = audit_comparator(tsk->euid, f->op, f->val);
  416. break;
  417. case AUDIT_SUID:
  418. result = audit_comparator(tsk->suid, f->op, f->val);
  419. break;
  420. case AUDIT_FSUID:
  421. result = audit_comparator(tsk->fsuid, f->op, f->val);
  422. break;
  423. case AUDIT_GID:
  424. result = audit_comparator(tsk->gid, f->op, f->val);
  425. break;
  426. case AUDIT_EGID:
  427. result = audit_comparator(tsk->egid, f->op, f->val);
  428. break;
  429. case AUDIT_SGID:
  430. result = audit_comparator(tsk->sgid, f->op, f->val);
  431. break;
  432. case AUDIT_FSGID:
  433. result = audit_comparator(tsk->fsgid, f->op, f->val);
  434. break;
  435. case AUDIT_PERS:
  436. result = audit_comparator(tsk->personality, f->op, f->val);
  437. break;
  438. case AUDIT_ARCH:
  439. if (ctx)
  440. result = audit_comparator(ctx->arch, f->op, f->val);
  441. break;
  442. case AUDIT_EXIT:
  443. if (ctx && ctx->return_valid)
  444. result = audit_comparator(ctx->return_code, f->op, f->val);
  445. break;
  446. case AUDIT_SUCCESS:
  447. if (ctx && ctx->return_valid) {
  448. if (f->val)
  449. result = audit_comparator(ctx->return_valid, f->op, AUDITSC_SUCCESS);
  450. else
  451. result = audit_comparator(ctx->return_valid, f->op, AUDITSC_FAILURE);
  452. }
  453. break;
  454. case AUDIT_DEVMAJOR:
  455. if (name)
  456. result = audit_comparator(MAJOR(name->dev),
  457. f->op, f->val);
  458. else if (ctx) {
  459. for (j = 0; j < ctx->name_count; j++) {
  460. if (audit_comparator(MAJOR(ctx->names[j].dev), f->op, f->val)) {
  461. ++result;
  462. break;
  463. }
  464. }
  465. }
  466. break;
  467. case AUDIT_DEVMINOR:
  468. if (name)
  469. result = audit_comparator(MINOR(name->dev),
  470. f->op, f->val);
  471. else if (ctx) {
  472. for (j = 0; j < ctx->name_count; j++) {
  473. if (audit_comparator(MINOR(ctx->names[j].dev), f->op, f->val)) {
  474. ++result;
  475. break;
  476. }
  477. }
  478. }
  479. break;
  480. case AUDIT_INODE:
  481. if (name)
  482. result = (name->ino == f->val);
  483. else if (ctx) {
  484. for (j = 0; j < ctx->name_count; j++) {
  485. if (audit_comparator(ctx->names[j].ino, f->op, f->val)) {
  486. ++result;
  487. break;
  488. }
  489. }
  490. }
  491. break;
  492. case AUDIT_WATCH:
  493. if (name && rule->watch->ino != (unsigned long)-1)
  494. result = (name->dev == rule->watch->dev &&
  495. name->ino == rule->watch->ino);
  496. break;
  497. case AUDIT_DIR:
  498. if (ctx)
  499. result = match_tree_refs(ctx, rule->tree);
  500. break;
  501. case AUDIT_LOGINUID:
  502. result = 0;
  503. if (ctx)
  504. result = audit_comparator(tsk->loginuid, f->op, f->val);
  505. break;
  506. case AUDIT_SUBJ_USER:
  507. case AUDIT_SUBJ_ROLE:
  508. case AUDIT_SUBJ_TYPE:
  509. case AUDIT_SUBJ_SEN:
  510. case AUDIT_SUBJ_CLR:
  511. /* NOTE: this may return negative values indicating
  512. a temporary error. We simply treat this as a
  513. match for now to avoid losing information that
  514. may be wanted. An error message will also be
  515. logged upon error */
  516. if (f->lsm_rule) {
  517. if (need_sid) {
  518. security_task_getsecid(tsk, &sid);
  519. need_sid = 0;
  520. }
  521. result = security_audit_rule_match(sid, f->type,
  522. f->op,
  523. f->lsm_rule,
  524. ctx);
  525. }
  526. break;
  527. case AUDIT_OBJ_USER:
  528. case AUDIT_OBJ_ROLE:
  529. case AUDIT_OBJ_TYPE:
  530. case AUDIT_OBJ_LEV_LOW:
  531. case AUDIT_OBJ_LEV_HIGH:
  532. /* The above note for AUDIT_SUBJ_USER...AUDIT_SUBJ_CLR
  533. also applies here */
  534. if (f->lsm_rule) {
  535. /* Find files that match */
  536. if (name) {
  537. result = security_audit_rule_match(
  538. name->osid, f->type, f->op,
  539. f->lsm_rule, ctx);
  540. } else if (ctx) {
  541. for (j = 0; j < ctx->name_count; j++) {
  542. if (security_audit_rule_match(
  543. ctx->names[j].osid,
  544. f->type, f->op,
  545. f->lsm_rule, ctx)) {
  546. ++result;
  547. break;
  548. }
  549. }
  550. }
  551. /* Find ipc objects that match */
  552. if (ctx) {
  553. struct audit_aux_data *aux;
  554. for (aux = ctx->aux; aux;
  555. aux = aux->next) {
  556. if (aux->type == AUDIT_IPC) {
  557. struct audit_aux_data_ipcctl *axi = (void *)aux;
  558. if (security_audit_rule_match(axi->osid, f->type, f->op, f->lsm_rule, ctx)) {
  559. ++result;
  560. break;
  561. }
  562. }
  563. }
  564. }
  565. }
  566. break;
  567. case AUDIT_ARG0:
  568. case AUDIT_ARG1:
  569. case AUDIT_ARG2:
  570. case AUDIT_ARG3:
  571. if (ctx)
  572. result = audit_comparator(ctx->argv[f->type-AUDIT_ARG0], f->op, f->val);
  573. break;
  574. case AUDIT_FILTERKEY:
  575. /* ignore this field for filtering */
  576. result = 1;
  577. break;
  578. case AUDIT_PERM:
  579. result = audit_match_perm(ctx, f->val);
  580. break;
  581. case AUDIT_FILETYPE:
  582. result = audit_match_filetype(ctx, f->val);
  583. break;
  584. }
  585. if (!result)
  586. return 0;
  587. }
  588. if (rule->filterkey && ctx)
  589. ctx->filterkey = kstrdup(rule->filterkey, GFP_ATOMIC);
  590. switch (rule->action) {
  591. case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
  592. case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
  593. }
  594. return 1;
  595. }
  596. /* At process creation time, we can determine if system-call auditing is
  597. * completely disabled for this task. Since we only have the task
  598. * structure at this point, we can only check uid and gid.
  599. */
  600. static enum audit_state audit_filter_task(struct task_struct *tsk)
  601. {
  602. struct audit_entry *e;
  603. enum audit_state state;
  604. rcu_read_lock();
  605. list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TASK], list) {
  606. if (audit_filter_rules(tsk, &e->rule, NULL, NULL, &state)) {
  607. rcu_read_unlock();
  608. return state;
  609. }
  610. }
  611. rcu_read_unlock();
  612. return AUDIT_BUILD_CONTEXT;
  613. }
  614. /* At syscall entry and exit time, this filter is called if the
  615. * audit_state is not low enough that auditing cannot take place, but is
  616. * also not high enough that we already know we have to write an audit
  617. * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT).
  618. */
  619. static enum audit_state audit_filter_syscall(struct task_struct *tsk,
  620. struct audit_context *ctx,
  621. struct list_head *list)
  622. {
  623. struct audit_entry *e;
  624. enum audit_state state;
  625. if (audit_pid && tsk->tgid == audit_pid)
  626. return AUDIT_DISABLED;
  627. rcu_read_lock();
  628. if (!list_empty(list)) {
  629. int word = AUDIT_WORD(ctx->major);
  630. int bit = AUDIT_BIT(ctx->major);
  631. list_for_each_entry_rcu(e, list, list) {
  632. if ((e->rule.mask[word] & bit) == bit &&
  633. audit_filter_rules(tsk, &e->rule, ctx, NULL,
  634. &state)) {
  635. rcu_read_unlock();
  636. return state;
  637. }
  638. }
  639. }
  640. rcu_read_unlock();
  641. return AUDIT_BUILD_CONTEXT;
  642. }
  643. /* At syscall exit time, this filter is called if any audit_names[] have been
  644. * collected during syscall processing. We only check rules in sublists at hash
  645. * buckets applicable to the inode numbers in audit_names[].
  646. * Regarding audit_state, same rules apply as for audit_filter_syscall().
  647. */
  648. enum audit_state audit_filter_inodes(struct task_struct *tsk,
  649. struct audit_context *ctx)
  650. {
  651. int i;
  652. struct audit_entry *e;
  653. enum audit_state state;
  654. if (audit_pid && tsk->tgid == audit_pid)
  655. return AUDIT_DISABLED;
  656. rcu_read_lock();
  657. for (i = 0; i < ctx->name_count; i++) {
  658. int word = AUDIT_WORD(ctx->major);
  659. int bit = AUDIT_BIT(ctx->major);
  660. struct audit_names *n = &ctx->names[i];
  661. int h = audit_hash_ino((u32)n->ino);
  662. struct list_head *list = &audit_inode_hash[h];
  663. if (list_empty(list))
  664. continue;
  665. list_for_each_entry_rcu(e, list, list) {
  666. if ((e->rule.mask[word] & bit) == bit &&
  667. audit_filter_rules(tsk, &e->rule, ctx, n, &state)) {
  668. rcu_read_unlock();
  669. return state;
  670. }
  671. }
  672. }
  673. rcu_read_unlock();
  674. return AUDIT_BUILD_CONTEXT;
  675. }
  676. void audit_set_auditable(struct audit_context *ctx)
  677. {
  678. ctx->auditable = 1;
  679. }
  680. static inline struct audit_context *audit_get_context(struct task_struct *tsk,
  681. int return_valid,
  682. int return_code)
  683. {
  684. struct audit_context *context = tsk->audit_context;
  685. if (likely(!context))
  686. return NULL;
  687. context->return_valid = return_valid;
  688. /*
  689. * we need to fix up the return code in the audit logs if the actual
  690. * return codes are later going to be fixed up by the arch specific
  691. * signal handlers
  692. *
  693. * This is actually a test for:
  694. * (rc == ERESTARTSYS ) || (rc == ERESTARTNOINTR) ||
  695. * (rc == ERESTARTNOHAND) || (rc == ERESTART_RESTARTBLOCK)
  696. *
  697. * but is faster than a bunch of ||
  698. */
  699. if (unlikely(return_code <= -ERESTARTSYS) &&
  700. (return_code >= -ERESTART_RESTARTBLOCK) &&
  701. (return_code != -ENOIOCTLCMD))
  702. context->return_code = -EINTR;
  703. else
  704. context->return_code = return_code;
  705. if (context->in_syscall && !context->dummy && !context->auditable) {
  706. enum audit_state state;
  707. state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_EXIT]);
  708. if (state == AUDIT_RECORD_CONTEXT) {
  709. context->auditable = 1;
  710. goto get_context;
  711. }
  712. state = audit_filter_inodes(tsk, context);
  713. if (state == AUDIT_RECORD_CONTEXT)
  714. context->auditable = 1;
  715. }
  716. get_context:
  717. tsk->audit_context = NULL;
  718. return context;
  719. }
  720. static inline void audit_free_names(struct audit_context *context)
  721. {
  722. int i;
  723. #if AUDIT_DEBUG == 2
  724. if (context->auditable
  725. ||context->put_count + context->ino_count != context->name_count) {
  726. printk(KERN_ERR "%s:%d(:%d): major=%d in_syscall=%d"
  727. " name_count=%d put_count=%d"
  728. " ino_count=%d [NOT freeing]\n",
  729. __FILE__, __LINE__,
  730. context->serial, context->major, context->in_syscall,
  731. context->name_count, context->put_count,
  732. context->ino_count);
  733. for (i = 0; i < context->name_count; i++) {
  734. printk(KERN_ERR "names[%d] = %p = %s\n", i,
  735. context->names[i].name,
  736. context->names[i].name ?: "(null)");
  737. }
  738. dump_stack();
  739. return;
  740. }
  741. #endif
  742. #if AUDIT_DEBUG
  743. context->put_count = 0;
  744. context->ino_count = 0;
  745. #endif
  746. for (i = 0; i < context->name_count; i++) {
  747. if (context->names[i].name && context->names[i].name_put)
  748. __putname(context->names[i].name);
  749. }
  750. context->name_count = 0;
  751. path_put(&context->pwd);
  752. context->pwd.dentry = NULL;
  753. context->pwd.mnt = NULL;
  754. }
  755. static inline void audit_free_aux(struct audit_context *context)
  756. {
  757. struct audit_aux_data *aux;
  758. while ((aux = context->aux)) {
  759. context->aux = aux->next;
  760. kfree(aux);
  761. }
  762. while ((aux = context->aux_pids)) {
  763. context->aux_pids = aux->next;
  764. kfree(aux);
  765. }
  766. }
  767. static inline void audit_zero_context(struct audit_context *context,
  768. enum audit_state state)
  769. {
  770. memset(context, 0, sizeof(*context));
  771. context->state = state;
  772. }
  773. static inline struct audit_context *audit_alloc_context(enum audit_state state)
  774. {
  775. struct audit_context *context;
  776. if (!(context = kmalloc(sizeof(*context), GFP_KERNEL)))
  777. return NULL;
  778. audit_zero_context(context, state);
  779. return context;
  780. }
  781. /**
  782. * audit_alloc - allocate an audit context block for a task
  783. * @tsk: task
  784. *
  785. * Filter on the task information and allocate a per-task audit context
  786. * if necessary. Doing so turns on system call auditing for the
  787. * specified task. This is called from copy_process, so no lock is
  788. * needed.
  789. */
  790. int audit_alloc(struct task_struct *tsk)
  791. {
  792. struct audit_context *context;
  793. enum audit_state state;
  794. if (likely(!audit_ever_enabled))
  795. return 0; /* Return if not auditing. */
  796. state = audit_filter_task(tsk);
  797. if (likely(state == AUDIT_DISABLED))
  798. return 0;
  799. if (!(context = audit_alloc_context(state))) {
  800. audit_log_lost("out of memory in audit_alloc");
  801. return -ENOMEM;
  802. }
  803. tsk->audit_context = context;
  804. set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
  805. return 0;
  806. }
  807. static inline void audit_free_context(struct audit_context *context)
  808. {
  809. struct audit_context *previous;
  810. int count = 0;
  811. do {
  812. previous = context->previous;
  813. if (previous || (count && count < 10)) {
  814. ++count;
  815. printk(KERN_ERR "audit(:%d): major=%d name_count=%d:"
  816. " freeing multiple contexts (%d)\n",
  817. context->serial, context->major,
  818. context->name_count, count);
  819. }
  820. audit_free_names(context);
  821. unroll_tree_refs(context, NULL, 0);
  822. free_tree_refs(context);
  823. audit_free_aux(context);
  824. kfree(context->filterkey);
  825. kfree(context);
  826. context = previous;
  827. } while (context);
  828. if (count >= 10)
  829. printk(KERN_ERR "audit: freed %d contexts\n", count);
  830. }
  831. void audit_log_task_context(struct audit_buffer *ab)
  832. {
  833. char *ctx = NULL;
  834. unsigned len;
  835. int error;
  836. u32 sid;
  837. security_task_getsecid(current, &sid);
  838. if (!sid)
  839. return;
  840. error = security_secid_to_secctx(sid, &ctx, &len);
  841. if (error) {
  842. if (error != -EINVAL)
  843. goto error_path;
  844. return;
  845. }
  846. audit_log_format(ab, " subj=%s", ctx);
  847. security_release_secctx(ctx, len);
  848. return;
  849. error_path:
  850. audit_panic("error in audit_log_task_context");
  851. return;
  852. }
  853. EXPORT_SYMBOL(audit_log_task_context);
  854. static void audit_log_task_info(struct audit_buffer *ab, struct task_struct *tsk)
  855. {
  856. char name[sizeof(tsk->comm)];
  857. struct mm_struct *mm = tsk->mm;
  858. struct vm_area_struct *vma;
  859. /* tsk == current */
  860. get_task_comm(name, tsk);
  861. audit_log_format(ab, " comm=");
  862. audit_log_untrustedstring(ab, name);
  863. if (mm) {
  864. down_read(&mm->mmap_sem);
  865. vma = mm->mmap;
  866. while (vma) {
  867. if ((vma->vm_flags & VM_EXECUTABLE) &&
  868. vma->vm_file) {
  869. audit_log_d_path(ab, "exe=",
  870. &vma->vm_file->f_path);
  871. break;
  872. }
  873. vma = vma->vm_next;
  874. }
  875. up_read(&mm->mmap_sem);
  876. }
  877. audit_log_task_context(ab);
  878. }
  879. static int audit_log_pid_context(struct audit_context *context, pid_t pid,
  880. uid_t auid, uid_t uid, unsigned int sessionid,
  881. u32 sid, char *comm)
  882. {
  883. struct audit_buffer *ab;
  884. char *ctx = NULL;
  885. u32 len;
  886. int rc = 0;
  887. ab = audit_log_start(context, GFP_KERNEL, AUDIT_OBJ_PID);
  888. if (!ab)
  889. return rc;
  890. audit_log_format(ab, "opid=%d oauid=%d ouid=%d oses=%d", pid, auid,
  891. uid, sessionid);
  892. if (security_secid_to_secctx(sid, &ctx, &len)) {
  893. audit_log_format(ab, " obj=(none)");
  894. rc = 1;
  895. } else {
  896. audit_log_format(ab, " obj=%s", ctx);
  897. security_release_secctx(ctx, len);
  898. }
  899. audit_log_format(ab, " ocomm=");
  900. audit_log_untrustedstring(ab, comm);
  901. audit_log_end(ab);
  902. return rc;
  903. }
  904. /*
  905. * to_send and len_sent accounting are very loose estimates. We aren't
  906. * really worried about a hard cap to MAX_EXECVE_AUDIT_LEN so much as being
  907. * within about 500 bytes (next page boundry)
  908. *
  909. * why snprintf? an int is up to 12 digits long. if we just assumed when
  910. * logging that a[%d]= was going to be 16 characters long we would be wasting
  911. * space in every audit message. In one 7500 byte message we can log up to
  912. * about 1000 min size arguments. That comes down to about 50% waste of space
  913. * if we didn't do the snprintf to find out how long arg_num_len was.
  914. */
  915. static int audit_log_single_execve_arg(struct audit_context *context,
  916. struct audit_buffer **ab,
  917. int arg_num,
  918. size_t *len_sent,
  919. const char __user *p,
  920. char *buf)
  921. {
  922. char arg_num_len_buf[12];
  923. const char __user *tmp_p = p;
  924. /* how many digits are in arg_num? 3 is the length of a=\n */
  925. size_t arg_num_len = snprintf(arg_num_len_buf, 12, "%d", arg_num) + 3;
  926. size_t len, len_left, to_send;
  927. size_t max_execve_audit_len = MAX_EXECVE_AUDIT_LEN;
  928. unsigned int i, has_cntl = 0, too_long = 0;
  929. int ret;
  930. /* strnlen_user includes the null we don't want to send */
  931. len_left = len = strnlen_user(p, MAX_ARG_STRLEN) - 1;
  932. /*
  933. * We just created this mm, if we can't find the strings
  934. * we just copied into it something is _very_ wrong. Similar
  935. * for strings that are too long, we should not have created
  936. * any.
  937. */
  938. if (unlikely((len == -1) || len > MAX_ARG_STRLEN - 1)) {
  939. WARN_ON(1);
  940. send_sig(SIGKILL, current, 0);
  941. return -1;
  942. }
  943. /* walk the whole argument looking for non-ascii chars */
  944. do {
  945. if (len_left > MAX_EXECVE_AUDIT_LEN)
  946. to_send = MAX_EXECVE_AUDIT_LEN;
  947. else
  948. to_send = len_left;
  949. ret = copy_from_user(buf, tmp_p, to_send);
  950. /*
  951. * There is no reason for this copy to be short. We just
  952. * copied them here, and the mm hasn't been exposed to user-
  953. * space yet.
  954. */
  955. if (ret) {
  956. WARN_ON(1);
  957. send_sig(SIGKILL, current, 0);
  958. return -1;
  959. }
  960. buf[to_send] = '\0';
  961. has_cntl = audit_string_contains_control(buf, to_send);
  962. if (has_cntl) {
  963. /*
  964. * hex messages get logged as 2 bytes, so we can only
  965. * send half as much in each message
  966. */
  967. max_execve_audit_len = MAX_EXECVE_AUDIT_LEN / 2;
  968. break;
  969. }
  970. len_left -= to_send;
  971. tmp_p += to_send;
  972. } while (len_left > 0);
  973. len_left = len;
  974. if (len > max_execve_audit_len)
  975. too_long = 1;
  976. /* rewalk the argument actually logging the message */
  977. for (i = 0; len_left > 0; i++) {
  978. int room_left;
  979. if (len_left > max_execve_audit_len)
  980. to_send = max_execve_audit_len;
  981. else
  982. to_send = len_left;
  983. /* do we have space left to send this argument in this ab? */
  984. room_left = MAX_EXECVE_AUDIT_LEN - arg_num_len - *len_sent;
  985. if (has_cntl)
  986. room_left -= (to_send * 2);
  987. else
  988. room_left -= to_send;
  989. if (room_left < 0) {
  990. *len_sent = 0;
  991. audit_log_end(*ab);
  992. *ab = audit_log_start(context, GFP_KERNEL, AUDIT_EXECVE);
  993. if (!*ab)
  994. return 0;
  995. }
  996. /*
  997. * first record needs to say how long the original string was
  998. * so we can be sure nothing was lost.
  999. */
  1000. if ((i == 0) && (too_long))
  1001. audit_log_format(*ab, "a%d_len=%zu ", arg_num,
  1002. has_cntl ? 2*len : len);
  1003. /*
  1004. * normally arguments are small enough to fit and we already
  1005. * filled buf above when we checked for control characters
  1006. * so don't bother with another copy_from_user
  1007. */
  1008. if (len >= max_execve_audit_len)
  1009. ret = copy_from_user(buf, p, to_send);
  1010. else
  1011. ret = 0;
  1012. if (ret) {
  1013. WARN_ON(1);
  1014. send_sig(SIGKILL, current, 0);
  1015. return -1;
  1016. }
  1017. buf[to_send] = '\0';
  1018. /* actually log it */
  1019. audit_log_format(*ab, "a%d", arg_num);
  1020. if (too_long)
  1021. audit_log_format(*ab, "[%d]", i);
  1022. audit_log_format(*ab, "=");
  1023. if (has_cntl)
  1024. audit_log_n_hex(*ab, buf, to_send);
  1025. else
  1026. audit_log_format(*ab, "\"%s\"", buf);
  1027. audit_log_format(*ab, "\n");
  1028. p += to_send;
  1029. len_left -= to_send;
  1030. *len_sent += arg_num_len;
  1031. if (has_cntl)
  1032. *len_sent += to_send * 2;
  1033. else
  1034. *len_sent += to_send;
  1035. }
  1036. /* include the null we didn't log */
  1037. return len + 1;
  1038. }
  1039. static void audit_log_execve_info(struct audit_context *context,
  1040. struct audit_buffer **ab,
  1041. struct audit_aux_data_execve *axi)
  1042. {
  1043. int i;
  1044. size_t len, len_sent = 0;
  1045. const char __user *p;
  1046. char *buf;
  1047. if (axi->mm != current->mm)
  1048. return; /* execve failed, no additional info */
  1049. p = (const char __user *)axi->mm->arg_start;
  1050. audit_log_format(*ab, "argc=%d ", axi->argc);
  1051. /*
  1052. * we need some kernel buffer to hold the userspace args. Just
  1053. * allocate one big one rather than allocating one of the right size
  1054. * for every single argument inside audit_log_single_execve_arg()
  1055. * should be <8k allocation so should be pretty safe.
  1056. */
  1057. buf = kmalloc(MAX_EXECVE_AUDIT_LEN + 1, GFP_KERNEL);
  1058. if (!buf) {
  1059. audit_panic("out of memory for argv string\n");
  1060. return;
  1061. }
  1062. for (i = 0; i < axi->argc; i++) {
  1063. len = audit_log_single_execve_arg(context, ab, i,
  1064. &len_sent, p, buf);
  1065. if (len <= 0)
  1066. break;
  1067. p += len;
  1068. }
  1069. kfree(buf);
  1070. }
  1071. static void audit_log_cap(struct audit_buffer *ab, char *prefix, kernel_cap_t *cap)
  1072. {
  1073. int i;
  1074. audit_log_format(ab, " %s=", prefix);
  1075. CAP_FOR_EACH_U32(i) {
  1076. audit_log_format(ab, "%08x", cap->cap[(_KERNEL_CAPABILITY_U32S-1) - i]);
  1077. }
  1078. }
  1079. static void audit_log_fcaps(struct audit_buffer *ab, struct audit_names *name)
  1080. {
  1081. kernel_cap_t *perm = &name->fcap.permitted;
  1082. kernel_cap_t *inh = &name->fcap.inheritable;
  1083. int log = 0;
  1084. if (!cap_isclear(*perm)) {
  1085. audit_log_cap(ab, "cap_fp", perm);
  1086. log = 1;
  1087. }
  1088. if (!cap_isclear(*inh)) {
  1089. audit_log_cap(ab, "cap_fi", inh);
  1090. log = 1;
  1091. }
  1092. if (log)
  1093. audit_log_format(ab, " cap_fe=%d cap_fver=%x", name->fcap.fE, name->fcap_ver);
  1094. }
  1095. static void audit_log_exit(struct audit_context *context, struct task_struct *tsk)
  1096. {
  1097. int i, call_panic = 0;
  1098. struct audit_buffer *ab;
  1099. struct audit_aux_data *aux;
  1100. const char *tty;
  1101. /* tsk == current */
  1102. context->pid = tsk->pid;
  1103. if (!context->ppid)
  1104. context->ppid = sys_getppid();
  1105. context->uid = tsk->uid;
  1106. context->gid = tsk->gid;
  1107. context->euid = tsk->euid;
  1108. context->suid = tsk->suid;
  1109. context->fsuid = tsk->fsuid;
  1110. context->egid = tsk->egid;
  1111. context->sgid = tsk->sgid;
  1112. context->fsgid = tsk->fsgid;
  1113. context->personality = tsk->personality;
  1114. ab = audit_log_start(context, GFP_KERNEL, AUDIT_SYSCALL);
  1115. if (!ab)
  1116. return; /* audit_panic has been called */
  1117. audit_log_format(ab, "arch=%x syscall=%d",
  1118. context->arch, context->major);
  1119. if (context->personality != PER_LINUX)
  1120. audit_log_format(ab, " per=%lx", context->personality);
  1121. if (context->return_valid)
  1122. audit_log_format(ab, " success=%s exit=%ld",
  1123. (context->return_valid==AUDITSC_SUCCESS)?"yes":"no",
  1124. context->return_code);
  1125. spin_lock_irq(&tsk->sighand->siglock);
  1126. if (tsk->signal && tsk->signal->tty && tsk->signal->tty->name)
  1127. tty = tsk->signal->tty->name;
  1128. else
  1129. tty = "(none)";
  1130. spin_unlock_irq(&tsk->sighand->siglock);
  1131. audit_log_format(ab,
  1132. " a0=%lx a1=%lx a2=%lx a3=%lx items=%d"
  1133. " ppid=%d pid=%d auid=%u uid=%u gid=%u"
  1134. " euid=%u suid=%u fsuid=%u"
  1135. " egid=%u sgid=%u fsgid=%u tty=%s ses=%u",
  1136. context->argv[0],
  1137. context->argv[1],
  1138. context->argv[2],
  1139. context->argv[3],
  1140. context->name_count,
  1141. context->ppid,
  1142. context->pid,
  1143. tsk->loginuid,
  1144. context->uid,
  1145. context->gid,
  1146. context->euid, context->suid, context->fsuid,
  1147. context->egid, context->sgid, context->fsgid, tty,
  1148. tsk->sessionid);
  1149. audit_log_task_info(ab, tsk);
  1150. if (context->filterkey) {
  1151. audit_log_format(ab, " key=");
  1152. audit_log_untrustedstring(ab, context->filterkey);
  1153. } else
  1154. audit_log_format(ab, " key=(null)");
  1155. audit_log_end(ab);
  1156. for (aux = context->aux; aux; aux = aux->next) {
  1157. ab = audit_log_start(context, GFP_KERNEL, aux->type);
  1158. if (!ab)
  1159. continue; /* audit_panic has been called */
  1160. switch (aux->type) {
  1161. case AUDIT_MQ_OPEN: {
  1162. struct audit_aux_data_mq_open *axi = (void *)aux;
  1163. audit_log_format(ab,
  1164. "oflag=0x%x mode=%#o mq_flags=0x%lx mq_maxmsg=%ld "
  1165. "mq_msgsize=%ld mq_curmsgs=%ld",
  1166. axi->oflag, axi->mode, axi->attr.mq_flags,
  1167. axi->attr.mq_maxmsg, axi->attr.mq_msgsize,
  1168. axi->attr.mq_curmsgs);
  1169. break; }
  1170. case AUDIT_MQ_SENDRECV: {
  1171. struct audit_aux_data_mq_sendrecv *axi = (void *)aux;
  1172. audit_log_format(ab,
  1173. "mqdes=%d msg_len=%zd msg_prio=%u "
  1174. "abs_timeout_sec=%ld abs_timeout_nsec=%ld",
  1175. axi->mqdes, axi->msg_len, axi->msg_prio,
  1176. axi->abs_timeout.tv_sec, axi->abs_timeout.tv_nsec);
  1177. break; }
  1178. case AUDIT_MQ_NOTIFY: {
  1179. struct audit_aux_data_mq_notify *axi = (void *)aux;
  1180. audit_log_format(ab,
  1181. "mqdes=%d sigev_signo=%d",
  1182. axi->mqdes,
  1183. axi->notification.sigev_signo);
  1184. break; }
  1185. case AUDIT_MQ_GETSETATTR: {
  1186. struct audit_aux_data_mq_getsetattr *axi = (void *)aux;
  1187. audit_log_format(ab,
  1188. "mqdes=%d mq_flags=0x%lx mq_maxmsg=%ld mq_msgsize=%ld "
  1189. "mq_curmsgs=%ld ",
  1190. axi->mqdes,
  1191. axi->mqstat.mq_flags, axi->mqstat.mq_maxmsg,
  1192. axi->mqstat.mq_msgsize, axi->mqstat.mq_curmsgs);
  1193. break; }
  1194. case AUDIT_IPC: {
  1195. struct audit_aux_data_ipcctl *axi = (void *)aux;
  1196. audit_log_format(ab,
  1197. "ouid=%u ogid=%u mode=%#o",
  1198. axi->uid, axi->gid, axi->mode);
  1199. if (axi->osid != 0) {
  1200. char *ctx = NULL;
  1201. u32 len;
  1202. if (security_secid_to_secctx(
  1203. axi->osid, &ctx, &len)) {
  1204. audit_log_format(ab, " osid=%u",
  1205. axi->osid);
  1206. call_panic = 1;
  1207. } else {
  1208. audit_log_format(ab, " obj=%s", ctx);
  1209. security_release_secctx(ctx, len);
  1210. }
  1211. }
  1212. break; }
  1213. case AUDIT_IPC_SET_PERM: {
  1214. struct audit_aux_data_ipcctl *axi = (void *)aux;
  1215. audit_log_format(ab,
  1216. "qbytes=%lx ouid=%u ogid=%u mode=%#o",
  1217. axi->qbytes, axi->uid, axi->gid, axi->mode);
  1218. break; }
  1219. case AUDIT_EXECVE: {
  1220. struct audit_aux_data_execve *axi = (void *)aux;
  1221. audit_log_execve_info(context, &ab, axi);
  1222. break; }
  1223. case AUDIT_SOCKETCALL: {
  1224. struct audit_aux_data_socketcall *axs = (void *)aux;
  1225. audit_log_format(ab, "nargs=%d", axs->nargs);
  1226. for (i=0; i<axs->nargs; i++)
  1227. audit_log_format(ab, " a%d=%lx", i, axs->args[i]);
  1228. break; }
  1229. case AUDIT_SOCKADDR: {
  1230. struct audit_aux_data_sockaddr *axs = (void *)aux;
  1231. audit_log_format(ab, "saddr=");
  1232. audit_log_n_hex(ab, axs->a, axs->len);
  1233. break; }
  1234. case AUDIT_FD_PAIR: {
  1235. struct audit_aux_data_fd_pair *axs = (void *)aux;
  1236. audit_log_format(ab, "fd0=%d fd1=%d", axs->fd[0], axs->fd[1]);
  1237. break; }
  1238. }
  1239. audit_log_end(ab);
  1240. }
  1241. for (aux = context->aux_pids; aux; aux = aux->next) {
  1242. struct audit_aux_data_pids *axs = (void *)aux;
  1243. for (i = 0; i < axs->pid_count; i++)
  1244. if (audit_log_pid_context(context, axs->target_pid[i],
  1245. axs->target_auid[i],
  1246. axs->target_uid[i],
  1247. axs->target_sessionid[i],
  1248. axs->target_sid[i],
  1249. axs->target_comm[i]))
  1250. call_panic = 1;
  1251. }
  1252. if (context->target_pid &&
  1253. audit_log_pid_context(context, context->target_pid,
  1254. context->target_auid, context->target_uid,
  1255. context->target_sessionid,
  1256. context->target_sid, context->target_comm))
  1257. call_panic = 1;
  1258. if (context->pwd.dentry && context->pwd.mnt) {
  1259. ab = audit_log_start(context, GFP_KERNEL, AUDIT_CWD);
  1260. if (ab) {
  1261. audit_log_d_path(ab, "cwd=", &context->pwd);
  1262. audit_log_end(ab);
  1263. }
  1264. }
  1265. for (i = 0; i < context->name_count; i++) {
  1266. struct audit_names *n = &context->names[i];
  1267. ab = audit_log_start(context, GFP_KERNEL, AUDIT_PATH);
  1268. if (!ab)
  1269. continue; /* audit_panic has been called */
  1270. audit_log_format(ab, "item=%d", i);
  1271. if (n->name) {
  1272. switch(n->name_len) {
  1273. case AUDIT_NAME_FULL:
  1274. /* log the full path */
  1275. audit_log_format(ab, " name=");
  1276. audit_log_untrustedstring(ab, n->name);
  1277. break;
  1278. case 0:
  1279. /* name was specified as a relative path and the
  1280. * directory component is the cwd */
  1281. audit_log_d_path(ab, " name=", &context->pwd);
  1282. break;
  1283. default:
  1284. /* log the name's directory component */
  1285. audit_log_format(ab, " name=");
  1286. audit_log_n_untrustedstring(ab, n->name,
  1287. n->name_len);
  1288. }
  1289. } else
  1290. audit_log_format(ab, " name=(null)");
  1291. if (n->ino != (unsigned long)-1) {
  1292. audit_log_format(ab, " inode=%lu"
  1293. " dev=%02x:%02x mode=%#o"
  1294. " ouid=%u ogid=%u rdev=%02x:%02x",
  1295. n->ino,
  1296. MAJOR(n->dev),
  1297. MINOR(n->dev),
  1298. n->mode,
  1299. n->uid,
  1300. n->gid,
  1301. MAJOR(n->rdev),
  1302. MINOR(n->rdev));
  1303. }
  1304. if (n->osid != 0) {
  1305. char *ctx = NULL;
  1306. u32 len;
  1307. if (security_secid_to_secctx(
  1308. n->osid, &ctx, &len)) {
  1309. audit_log_format(ab, " osid=%u", n->osid);
  1310. call_panic = 2;
  1311. } else {
  1312. audit_log_format(ab, " obj=%s", ctx);
  1313. security_release_secctx(ctx, len);
  1314. }
  1315. }
  1316. audit_log_fcaps(ab, n);
  1317. audit_log_end(ab);
  1318. }
  1319. /* Send end of event record to help user space know we are finished */
  1320. ab = audit_log_start(context, GFP_KERNEL, AUDIT_EOE);
  1321. if (ab)
  1322. audit_log_end(ab);
  1323. if (call_panic)
  1324. audit_panic("error converting sid to string");
  1325. }
  1326. /**
  1327. * audit_free - free a per-task audit context
  1328. * @tsk: task whose audit context block to free
  1329. *
  1330. * Called from copy_process and do_exit
  1331. */
  1332. void audit_free(struct task_struct *tsk)
  1333. {
  1334. struct audit_context *context;
  1335. context = audit_get_context(tsk, 0, 0);
  1336. if (likely(!context))
  1337. return;
  1338. /* Check for system calls that do not go through the exit
  1339. * function (e.g., exit_group), then free context block.
  1340. * We use GFP_ATOMIC here because we might be doing this
  1341. * in the context of the idle thread */
  1342. /* that can happen only if we are called from do_exit() */
  1343. if (context->in_syscall && context->auditable)
  1344. audit_log_exit(context, tsk);
  1345. audit_free_context(context);
  1346. }
  1347. /**
  1348. * audit_syscall_entry - fill in an audit record at syscall entry
  1349. * @tsk: task being audited
  1350. * @arch: architecture type
  1351. * @major: major syscall type (function)
  1352. * @a1: additional syscall register 1
  1353. * @a2: additional syscall register 2
  1354. * @a3: additional syscall register 3
  1355. * @a4: additional syscall register 4
  1356. *
  1357. * Fill in audit context at syscall entry. This only happens if the
  1358. * audit context was created when the task was created and the state or
  1359. * filters demand the audit context be built. If the state from the
  1360. * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT,
  1361. * then the record will be written at syscall exit time (otherwise, it
  1362. * will only be written if another part of the kernel requests that it
  1363. * be written).
  1364. */
  1365. void audit_syscall_entry(int arch, int major,
  1366. unsigned long a1, unsigned long a2,
  1367. unsigned long a3, unsigned long a4)
  1368. {
  1369. struct task_struct *tsk = current;
  1370. struct audit_context *context = tsk->audit_context;
  1371. enum audit_state state;
  1372. if (unlikely(!context))
  1373. return;
  1374. /*
  1375. * This happens only on certain architectures that make system
  1376. * calls in kernel_thread via the entry.S interface, instead of
  1377. * with direct calls. (If you are porting to a new
  1378. * architecture, hitting this condition can indicate that you
  1379. * got the _exit/_leave calls backward in entry.S.)
  1380. *
  1381. * i386 no
  1382. * x86_64 no
  1383. * ppc64 yes (see arch/powerpc/platforms/iseries/misc.S)
  1384. *
  1385. * This also happens with vm86 emulation in a non-nested manner
  1386. * (entries without exits), so this case must be caught.
  1387. */
  1388. if (context->in_syscall) {
  1389. struct audit_context *newctx;
  1390. #if AUDIT_DEBUG
  1391. printk(KERN_ERR
  1392. "audit(:%d) pid=%d in syscall=%d;"
  1393. " entering syscall=%d\n",
  1394. context->serial, tsk->pid, context->major, major);
  1395. #endif
  1396. newctx = audit_alloc_context(context->state);
  1397. if (newctx) {
  1398. newctx->previous = context;
  1399. context = newctx;
  1400. tsk->audit_context = newctx;
  1401. } else {
  1402. /* If we can't alloc a new context, the best we
  1403. * can do is to leak memory (any pending putname
  1404. * will be lost). The only other alternative is
  1405. * to abandon auditing. */
  1406. audit_zero_context(context, context->state);
  1407. }
  1408. }
  1409. BUG_ON(context->in_syscall || context->name_count);
  1410. if (!audit_enabled)
  1411. return;
  1412. context->arch = arch;
  1413. context->major = major;
  1414. context->argv[0] = a1;
  1415. context->argv[1] = a2;
  1416. context->argv[2] = a3;
  1417. context->argv[3] = a4;
  1418. state = context->state;
  1419. context->dummy = !audit_n_rules;
  1420. if (!context->dummy && (state == AUDIT_SETUP_CONTEXT || state == AUDIT_BUILD_CONTEXT))
  1421. state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_ENTRY]);
  1422. if (likely(state == AUDIT_DISABLED))
  1423. return;
  1424. context->serial = 0;
  1425. context->ctime = CURRENT_TIME;
  1426. context->in_syscall = 1;
  1427. context->auditable = !!(state == AUDIT_RECORD_CONTEXT);
  1428. context->ppid = 0;
  1429. }
  1430. /**
  1431. * audit_syscall_exit - deallocate audit context after a system call
  1432. * @tsk: task being audited
  1433. * @valid: success/failure flag
  1434. * @return_code: syscall return value
  1435. *
  1436. * Tear down after system call. If the audit context has been marked as
  1437. * auditable (either because of the AUDIT_RECORD_CONTEXT state from
  1438. * filtering, or because some other part of the kernel write an audit
  1439. * message), then write out the syscall information. In call cases,
  1440. * free the names stored from getname().
  1441. */
  1442. void audit_syscall_exit(int valid, long return_code)
  1443. {
  1444. struct task_struct *tsk = current;
  1445. struct audit_context *context;
  1446. context = audit_get_context(tsk, valid, return_code);
  1447. if (likely(!context))
  1448. return;
  1449. if (context->in_syscall && context->auditable)
  1450. audit_log_exit(context, tsk);
  1451. context->in_syscall = 0;
  1452. context->auditable = 0;
  1453. if (context->previous) {
  1454. struct audit_context *new_context = context->previous;
  1455. context->previous = NULL;
  1456. audit_free_context(context);
  1457. tsk->audit_context = new_context;
  1458. } else {
  1459. audit_free_names(context);
  1460. unroll_tree_refs(context, NULL, 0);
  1461. audit_free_aux(context);
  1462. context->aux = NULL;
  1463. context->aux_pids = NULL;
  1464. context->target_pid = 0;
  1465. context->target_sid = 0;
  1466. kfree(context->filterkey);
  1467. context->filterkey = NULL;
  1468. tsk->audit_context = context;
  1469. }
  1470. }
  1471. static inline void handle_one(const struct inode *inode)
  1472. {
  1473. #ifdef CONFIG_AUDIT_TREE
  1474. struct audit_context *context;
  1475. struct audit_tree_refs *p;
  1476. struct audit_chunk *chunk;
  1477. int count;
  1478. if (likely(list_empty(&inode->inotify_watches)))
  1479. return;
  1480. context = current->audit_context;
  1481. p = context->trees;
  1482. count = context->tree_count;
  1483. rcu_read_lock();
  1484. chunk = audit_tree_lookup(inode);
  1485. rcu_read_unlock();
  1486. if (!chunk)
  1487. return;
  1488. if (likely(put_tree_ref(context, chunk)))
  1489. return;
  1490. if (unlikely(!grow_tree_refs(context))) {
  1491. printk(KERN_WARNING "out of memory, audit has lost a tree reference\n");
  1492. audit_set_auditable(context);
  1493. audit_put_chunk(chunk);
  1494. unroll_tree_refs(context, p, count);
  1495. return;
  1496. }
  1497. put_tree_ref(context, chunk);
  1498. #endif
  1499. }
  1500. static void handle_path(const struct dentry *dentry)
  1501. {
  1502. #ifdef CONFIG_AUDIT_TREE
  1503. struct audit_context *context;
  1504. struct audit_tree_refs *p;
  1505. const struct dentry *d, *parent;
  1506. struct audit_chunk *drop;
  1507. unsigned long seq;
  1508. int count;
  1509. context = current->audit_context;
  1510. p = context->trees;
  1511. count = context->tree_count;
  1512. retry:
  1513. drop = NULL;
  1514. d = dentry;
  1515. rcu_read_lock();
  1516. seq = read_seqbegin(&rename_lock);
  1517. for(;;) {
  1518. struct inode *inode = d->d_inode;
  1519. if (inode && unlikely(!list_empty(&inode->inotify_watches))) {
  1520. struct audit_chunk *chunk;
  1521. chunk = audit_tree_lookup(inode);
  1522. if (chunk) {
  1523. if (unlikely(!put_tree_ref(context, chunk))) {
  1524. drop = chunk;
  1525. break;
  1526. }
  1527. }
  1528. }
  1529. parent = d->d_parent;
  1530. if (parent == d)
  1531. break;
  1532. d = parent;
  1533. }
  1534. if (unlikely(read_seqretry(&rename_lock, seq) || drop)) { /* in this order */
  1535. rcu_read_unlock();
  1536. if (!drop) {
  1537. /* just a race with rename */
  1538. unroll_tree_refs(context, p, count);
  1539. goto retry;
  1540. }
  1541. audit_put_chunk(drop);
  1542. if (grow_tree_refs(context)) {
  1543. /* OK, got more space */
  1544. unroll_tree_refs(context, p, count);
  1545. goto retry;
  1546. }
  1547. /* too bad */
  1548. printk(KERN_WARNING
  1549. "out of memory, audit has lost a tree reference\n");
  1550. unroll_tree_refs(context, p, count);
  1551. audit_set_auditable(context);
  1552. return;
  1553. }
  1554. rcu_read_unlock();
  1555. #endif
  1556. }
  1557. /**
  1558. * audit_getname - add a name to the list
  1559. * @name: name to add
  1560. *
  1561. * Add a name to the list of audit names for this context.
  1562. * Called from fs/namei.c:getname().
  1563. */
  1564. void __audit_getname(const char *name)
  1565. {
  1566. struct audit_context *context = current->audit_context;
  1567. if (IS_ERR(name) || !name)
  1568. return;
  1569. if (!context->in_syscall) {
  1570. #if AUDIT_DEBUG == 2
  1571. printk(KERN_ERR "%s:%d(:%d): ignoring getname(%p)\n",
  1572. __FILE__, __LINE__, context->serial, name);
  1573. dump_stack();
  1574. #endif
  1575. return;
  1576. }
  1577. BUG_ON(context->name_count >= AUDIT_NAMES);
  1578. context->names[context->name_count].name = name;
  1579. context->names[context->name_count].name_len = AUDIT_NAME_FULL;
  1580. context->names[context->name_count].name_put = 1;
  1581. context->names[context->name_count].ino = (unsigned long)-1;
  1582. context->names[context->name_count].osid = 0;
  1583. ++context->name_count;
  1584. if (!context->pwd.dentry) {
  1585. read_lock(&current->fs->lock);
  1586. context->pwd = current->fs->pwd;
  1587. path_get(&current->fs->pwd);
  1588. read_unlock(&current->fs->lock);
  1589. }
  1590. }
  1591. /* audit_putname - intercept a putname request
  1592. * @name: name to intercept and delay for putname
  1593. *
  1594. * If we have stored the name from getname in the audit context,
  1595. * then we delay the putname until syscall exit.
  1596. * Called from include/linux/fs.h:putname().
  1597. */
  1598. void audit_putname(const char *name)
  1599. {
  1600. struct audit_context *context = current->audit_context;
  1601. BUG_ON(!context);
  1602. if (!context->in_syscall) {
  1603. #if AUDIT_DEBUG == 2
  1604. printk(KERN_ERR "%s:%d(:%d): __putname(%p)\n",
  1605. __FILE__, __LINE__, context->serial, name);
  1606. if (context->name_count) {
  1607. int i;
  1608. for (i = 0; i < context->name_count; i++)
  1609. printk(KERN_ERR "name[%d] = %p = %s\n", i,
  1610. context->names[i].name,
  1611. context->names[i].name ?: "(null)");
  1612. }
  1613. #endif
  1614. __putname(name);
  1615. }
  1616. #if AUDIT_DEBUG
  1617. else {
  1618. ++context->put_count;
  1619. if (context->put_count > context->name_count) {
  1620. printk(KERN_ERR "%s:%d(:%d): major=%d"
  1621. " in_syscall=%d putname(%p) name_count=%d"
  1622. " put_count=%d\n",
  1623. __FILE__, __LINE__,
  1624. context->serial, context->major,
  1625. context->in_syscall, name, context->name_count,
  1626. context->put_count);
  1627. dump_stack();
  1628. }
  1629. }
  1630. #endif
  1631. }
  1632. static int audit_inc_name_count(struct audit_context *context,
  1633. const struct inode *inode)
  1634. {
  1635. if (context->name_count >= AUDIT_NAMES) {
  1636. if (inode)
  1637. printk(KERN_DEBUG "name_count maxed, losing inode data: "
  1638. "dev=%02x:%02x, inode=%lu\n",
  1639. MAJOR(inode->i_sb->s_dev),
  1640. MINOR(inode->i_sb->s_dev),
  1641. inode->i_ino);
  1642. else
  1643. printk(KERN_DEBUG "name_count maxed, losing inode data\n");
  1644. return 1;
  1645. }
  1646. context->name_count++;
  1647. #if AUDIT_DEBUG
  1648. context->ino_count++;
  1649. #endif
  1650. return 0;
  1651. }
  1652. static inline int audit_copy_fcaps(struct audit_names *name, const struct dentry *dentry)
  1653. {
  1654. struct cpu_vfs_cap_data caps;
  1655. int rc;
  1656. memset(&name->fcap.permitted, 0, sizeof(kernel_cap_t));
  1657. memset(&name->fcap.inheritable, 0, sizeof(kernel_cap_t));
  1658. name->fcap.fE = 0;
  1659. name->fcap_ver = 0;
  1660. if (!dentry)
  1661. return 0;
  1662. rc = get_vfs_caps_from_disk(dentry, &caps);
  1663. if (rc)
  1664. return rc;
  1665. name->fcap.permitted = caps.permitted;
  1666. name->fcap.inheritable = caps.inheritable;
  1667. name->fcap.fE = !!(caps.magic_etc & VFS_CAP_FLAGS_EFFECTIVE);
  1668. name->fcap_ver = (caps.magic_etc & VFS_CAP_REVISION_MASK) >> VFS_CAP_REVISION_SHIFT;
  1669. return 0;
  1670. }
  1671. /* Copy inode data into an audit_names. */
  1672. static void audit_copy_inode(struct audit_names *name, const struct dentry *dentry,
  1673. const struct inode *inode)
  1674. {
  1675. name->ino = inode->i_ino;
  1676. name->dev = inode->i_sb->s_dev;
  1677. name->mode = inode->i_mode;
  1678. name->uid = inode->i_uid;
  1679. name->gid = inode->i_gid;
  1680. name->rdev = inode->i_rdev;
  1681. security_inode_getsecid(inode, &name->osid);
  1682. audit_copy_fcaps(name, dentry);
  1683. }
  1684. /**
  1685. * audit_inode - store the inode and device from a lookup
  1686. * @name: name being audited
  1687. * @dentry: dentry being audited
  1688. *
  1689. * Called from fs/namei.c:path_lookup().
  1690. */
  1691. void __audit_inode(const char *name, const struct dentry *dentry)
  1692. {
  1693. int idx;
  1694. struct audit_context *context = current->audit_context;
  1695. const struct inode *inode = dentry->d_inode;
  1696. if (!context->in_syscall)
  1697. return;
  1698. if (context->name_count
  1699. && context->names[context->name_count-1].name
  1700. && context->names[context->name_count-1].name == name)
  1701. idx = context->name_count - 1;
  1702. else if (context->name_count > 1
  1703. && context->names[context->name_count-2].name
  1704. && context->names[context->name_count-2].name == name)
  1705. idx = context->name_count - 2;
  1706. else {
  1707. /* FIXME: how much do we care about inodes that have no
  1708. * associated name? */
  1709. if (audit_inc_name_count(context, inode))
  1710. return;
  1711. idx = context->name_count - 1;
  1712. context->names[idx].name = NULL;
  1713. }
  1714. handle_path(dentry);
  1715. audit_copy_inode(&context->names[idx], dentry, inode);
  1716. }
  1717. /**
  1718. * audit_inode_child - collect inode info for created/removed objects
  1719. * @dname: inode's dentry name
  1720. * @dentry: dentry being audited
  1721. * @parent: inode of dentry parent
  1722. *
  1723. * For syscalls that create or remove filesystem objects, audit_inode
  1724. * can only collect information for the filesystem object's parent.
  1725. * This call updates the audit context with the child's information.
  1726. * Syscalls that create a new filesystem object must be hooked after
  1727. * the object is created. Syscalls that remove a filesystem object
  1728. * must be hooked prior, in order to capture the target inode during
  1729. * unsuccessful attempts.
  1730. */
  1731. void __audit_inode_child(const char *dname, const struct dentry *dentry,
  1732. const struct inode *parent)
  1733. {
  1734. int idx;
  1735. struct audit_context *context = current->audit_context;
  1736. const char *found_parent = NULL, *found_child = NULL;
  1737. const struct inode *inode = dentry->d_inode;
  1738. int dirlen = 0;
  1739. if (!context->in_syscall)
  1740. return;
  1741. if (inode)
  1742. handle_one(inode);
  1743. /* determine matching parent */
  1744. if (!dname)
  1745. goto add_names;
  1746. /* parent is more likely, look for it first */
  1747. for (idx = 0; idx < context->name_count; idx++) {
  1748. struct audit_names *n = &context->names[idx];
  1749. if (!n->name)
  1750. continue;
  1751. if (n->ino == parent->i_ino &&
  1752. !audit_compare_dname_path(dname, n->name, &dirlen)) {
  1753. n->name_len = dirlen; /* update parent data in place */
  1754. found_parent = n->name;
  1755. goto add_names;
  1756. }
  1757. }
  1758. /* no matching parent, look for matching child */
  1759. for (idx = 0; idx < context->name_count; idx++) {
  1760. struct audit_names *n = &context->names[idx];
  1761. if (!n->name)
  1762. continue;
  1763. /* strcmp() is the more likely scenario */
  1764. if (!strcmp(dname, n->name) ||
  1765. !audit_compare_dname_path(dname, n->name, &dirlen)) {
  1766. if (inode)
  1767. audit_copy_inode(n, NULL, inode);
  1768. else
  1769. n->ino = (unsigned long)-1;
  1770. found_child = n->name;
  1771. goto add_names;
  1772. }
  1773. }
  1774. add_names:
  1775. if (!found_parent) {
  1776. if (audit_inc_name_count(context, parent))
  1777. return;
  1778. idx = context->name_count - 1;
  1779. context->names[idx].name = NULL;
  1780. audit_copy_inode(&context->names[idx], NULL, parent);
  1781. }
  1782. if (!found_child) {
  1783. if (audit_inc_name_count(context, inode))
  1784. return;
  1785. idx = context->name_count - 1;
  1786. /* Re-use the name belonging to the slot for a matching parent
  1787. * directory. All names for this context are relinquished in
  1788. * audit_free_names() */
  1789. if (found_parent) {
  1790. context->names[idx].name = found_parent;
  1791. context->names[idx].name_len = AUDIT_NAME_FULL;
  1792. /* don't call __putname() */
  1793. context->names[idx].name_put = 0;
  1794. } else {
  1795. context->names[idx].name = NULL;
  1796. }
  1797. if (inode)
  1798. audit_copy_inode(&context->names[idx], NULL, inode);
  1799. else
  1800. context->names[idx].ino = (unsigned long)-1;
  1801. }
  1802. }
  1803. EXPORT_SYMBOL_GPL(__audit_inode_child);
  1804. /**
  1805. * auditsc_get_stamp - get local copies of audit_context values
  1806. * @ctx: audit_context for the task
  1807. * @t: timespec to store time recorded in the audit_context
  1808. * @serial: serial value that is recorded in the audit_context
  1809. *
  1810. * Also sets the context as auditable.
  1811. */
  1812. void auditsc_get_stamp(struct audit_context *ctx,
  1813. struct timespec *t, unsigned int *serial)
  1814. {
  1815. if (!ctx->serial)
  1816. ctx->serial = audit_serial();
  1817. t->tv_sec = ctx->ctime.tv_sec;
  1818. t->tv_nsec = ctx->ctime.tv_nsec;
  1819. *serial = ctx->serial;
  1820. ctx->auditable = 1;
  1821. }
  1822. /* global counter which is incremented every time something logs in */
  1823. static atomic_t session_id = ATOMIC_INIT(0);
  1824. /**
  1825. * audit_set_loginuid - set a task's audit_context loginuid
  1826. * @task: task whose audit context is being modified
  1827. * @loginuid: loginuid value
  1828. *
  1829. * Returns 0.
  1830. *
  1831. * Called (set) from fs/proc/base.c::proc_loginuid_write().
  1832. */
  1833. int audit_set_loginuid(struct task_struct *task, uid_t loginuid)
  1834. {
  1835. unsigned int sessionid = atomic_inc_return(&session_id);
  1836. struct audit_context *context = task->audit_context;
  1837. if (context && context->in_syscall) {
  1838. struct audit_buffer *ab;
  1839. ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_LOGIN);
  1840. if (ab) {
  1841. audit_log_format(ab, "login pid=%d uid=%u "
  1842. "old auid=%u new auid=%u"
  1843. " old ses=%u new ses=%u",
  1844. task->pid, task->uid,
  1845. task->loginuid, loginuid,
  1846. task->sessionid, sessionid);
  1847. audit_log_end(ab);
  1848. }
  1849. }
  1850. task->sessionid = sessionid;
  1851. task->loginuid = loginuid;
  1852. return 0;
  1853. }
  1854. /**
  1855. * __audit_mq_open - record audit data for a POSIX MQ open
  1856. * @oflag: open flag
  1857. * @mode: mode bits
  1858. * @u_attr: queue attributes
  1859. *
  1860. * Returns 0 for success or NULL context or < 0 on error.
  1861. */
  1862. int __audit_mq_open(int oflag, mode_t mode, struct mq_attr __user *u_attr)
  1863. {
  1864. struct audit_aux_data_mq_open *ax;
  1865. struct audit_context *context = current->audit_context;
  1866. if (!audit_enabled)
  1867. return 0;
  1868. if (likely(!context))
  1869. return 0;
  1870. ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
  1871. if (!ax)
  1872. return -ENOMEM;
  1873. if (u_attr != NULL) {
  1874. if (copy_from_user(&ax->attr, u_attr, sizeof(ax->attr))) {
  1875. kfree(ax);
  1876. return -EFAULT;
  1877. }
  1878. } else
  1879. memset(&ax->attr, 0, sizeof(ax->attr));
  1880. ax->oflag = oflag;
  1881. ax->mode = mode;
  1882. ax->d.type = AUDIT_MQ_OPEN;
  1883. ax->d.next = context->aux;
  1884. context->aux = (void *)ax;
  1885. return 0;
  1886. }
  1887. /**
  1888. * __audit_mq_timedsend - record audit data for a POSIX MQ timed send
  1889. * @mqdes: MQ descriptor
  1890. * @msg_len: Message length
  1891. * @msg_prio: Message priority
  1892. * @u_abs_timeout: Message timeout in absolute time
  1893. *
  1894. * Returns 0 for success or NULL context or < 0 on error.
  1895. */
  1896. int __audit_mq_timedsend(mqd_t mqdes, size_t msg_len, unsigned int msg_prio,
  1897. const struct timespec __user *u_abs_timeout)
  1898. {
  1899. struct audit_aux_data_mq_sendrecv *ax;
  1900. struct audit_context *context = current->audit_context;
  1901. if (!audit_enabled)
  1902. return 0;
  1903. if (likely(!context))
  1904. return 0;
  1905. ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
  1906. if (!ax)
  1907. return -ENOMEM;
  1908. if (u_abs_timeout != NULL) {
  1909. if (copy_from_user(&ax->abs_timeout, u_abs_timeout, sizeof(ax->abs_timeout))) {
  1910. kfree(ax);
  1911. return -EFAULT;
  1912. }
  1913. } else
  1914. memset(&ax->abs_timeout, 0, sizeof(ax->abs_timeout));
  1915. ax->mqdes = mqdes;
  1916. ax->msg_len = msg_len;
  1917. ax->msg_prio = msg_prio;
  1918. ax->d.type = AUDIT_MQ_SENDRECV;
  1919. ax->d.next = context->aux;
  1920. context->aux = (void *)ax;
  1921. return 0;
  1922. }
  1923. /**
  1924. * __audit_mq_timedreceive - record audit data for a POSIX MQ timed receive
  1925. * @mqdes: MQ descriptor
  1926. * @msg_len: Message length
  1927. * @u_msg_prio: Message priority
  1928. * @u_abs_timeout: Message timeout in absolute time
  1929. *
  1930. * Returns 0 for success or NULL context or < 0 on error.
  1931. */
  1932. int __audit_mq_timedreceive(mqd_t mqdes, size_t msg_len,
  1933. unsigned int __user *u_msg_prio,
  1934. const struct timespec __user *u_abs_timeout)
  1935. {
  1936. struct audit_aux_data_mq_sendrecv *ax;
  1937. struct audit_context *context = current->audit_context;
  1938. if (!audit_enabled)
  1939. return 0;
  1940. if (likely(!context))
  1941. return 0;
  1942. ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
  1943. if (!ax)
  1944. return -ENOMEM;
  1945. if (u_msg_prio != NULL) {
  1946. if (get_user(ax->msg_prio, u_msg_prio)) {
  1947. kfree(ax);
  1948. return -EFAULT;
  1949. }
  1950. } else
  1951. ax->msg_prio = 0;
  1952. if (u_abs_timeout != NULL) {
  1953. if (copy_from_user(&ax->abs_timeout, u_abs_timeout, sizeof(ax->abs_timeout))) {
  1954. kfree(ax);
  1955. return -EFAULT;
  1956. }
  1957. } else
  1958. memset(&ax->abs_timeout, 0, sizeof(ax->abs_timeout));
  1959. ax->mqdes = mqdes;
  1960. ax->msg_len = msg_len;
  1961. ax->d.type = AUDIT_MQ_SENDRECV;
  1962. ax->d.next = context->aux;
  1963. context->aux = (void *)ax;
  1964. return 0;
  1965. }
  1966. /**
  1967. * __audit_mq_notify - record audit data for a POSIX MQ notify
  1968. * @mqdes: MQ descriptor
  1969. * @u_notification: Notification event
  1970. *
  1971. * Returns 0 for success or NULL context or < 0 on error.
  1972. */
  1973. int __audit_mq_notify(mqd_t mqdes, const struct sigevent __user *u_notification)
  1974. {
  1975. struct audit_aux_data_mq_notify *ax;
  1976. struct audit_context *context = current->audit_context;
  1977. if (!audit_enabled)
  1978. return 0;
  1979. if (likely(!context))
  1980. return 0;
  1981. ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
  1982. if (!ax)
  1983. return -ENOMEM;
  1984. if (u_notification != NULL) {
  1985. if (copy_from_user(&ax->notification, u_notification, sizeof(ax->notification))) {
  1986. kfree(ax);
  1987. return -EFAULT;
  1988. }
  1989. } else
  1990. memset(&ax->notification, 0, sizeof(ax->notification));
  1991. ax->mqdes = mqdes;
  1992. ax->d.type = AUDIT_MQ_NOTIFY;
  1993. ax->d.next = context->aux;
  1994. context->aux = (void *)ax;
  1995. return 0;
  1996. }
  1997. /**
  1998. * __audit_mq_getsetattr - record audit data for a POSIX MQ get/set attribute
  1999. * @mqdes: MQ descriptor
  2000. * @mqstat: MQ flags
  2001. *
  2002. * Returns 0 for success or NULL context or < 0 on error.
  2003. */
  2004. int __audit_mq_getsetattr(mqd_t mqdes, struct mq_attr *mqstat)
  2005. {
  2006. struct audit_aux_data_mq_getsetattr *ax;
  2007. struct audit_context *context = current->audit_context;
  2008. if (!audit_enabled)
  2009. return 0;
  2010. if (likely(!context))
  2011. return 0;
  2012. ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
  2013. if (!ax)
  2014. return -ENOMEM;
  2015. ax->mqdes = mqdes;
  2016. ax->mqstat = *mqstat;
  2017. ax->d.type = AUDIT_MQ_GETSETATTR;
  2018. ax->d.next = context->aux;
  2019. context->aux = (void *)ax;
  2020. return 0;
  2021. }
  2022. /**
  2023. * audit_ipc_obj - record audit data for ipc object
  2024. * @ipcp: ipc permissions
  2025. *
  2026. * Returns 0 for success or NULL context or < 0 on error.
  2027. */
  2028. int __audit_ipc_obj(struct kern_ipc_perm *ipcp)
  2029. {
  2030. struct audit_aux_data_ipcctl *ax;
  2031. struct audit_context *context = current->audit_context;
  2032. ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
  2033. if (!ax)
  2034. return -ENOMEM;
  2035. ax->uid = ipcp->uid;
  2036. ax->gid = ipcp->gid;
  2037. ax->mode = ipcp->mode;
  2038. security_ipc_getsecid(ipcp, &ax->osid);
  2039. ax->d.type = AUDIT_IPC;
  2040. ax->d.next = context->aux;
  2041. context->aux = (void *)ax;
  2042. return 0;
  2043. }
  2044. /**
  2045. * audit_ipc_set_perm - record audit data for new ipc permissions
  2046. * @qbytes: msgq bytes
  2047. * @uid: msgq user id
  2048. * @gid: msgq group id
  2049. * @mode: msgq mode (permissions)
  2050. *
  2051. * Returns 0 for success or NULL context or < 0 on error.
  2052. */
  2053. int __audit_ipc_set_perm(unsigned long qbytes, uid_t uid, gid_t gid, mode_t mode)
  2054. {
  2055. struct audit_aux_data_ipcctl *ax;
  2056. struct audit_context *context = current->audit_context;
  2057. ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
  2058. if (!ax)
  2059. return -ENOMEM;
  2060. ax->qbytes = qbytes;
  2061. ax->uid = uid;
  2062. ax->gid = gid;
  2063. ax->mode = mode;
  2064. ax->d.type = AUDIT_IPC_SET_PERM;
  2065. ax->d.next = context->aux;
  2066. context->aux = (void *)ax;
  2067. return 0;
  2068. }
  2069. int audit_bprm(struct linux_binprm *bprm)
  2070. {
  2071. struct audit_aux_data_execve *ax;
  2072. struct audit_context *context = current->audit_context;
  2073. if (likely(!audit_enabled || !context || context->dummy))
  2074. return 0;
  2075. ax = kmalloc(sizeof(*ax), GFP_KERNEL);
  2076. if (!ax)
  2077. return -ENOMEM;
  2078. ax->argc = bprm->argc;
  2079. ax->envc = bprm->envc;
  2080. ax->mm = bprm->mm;
  2081. ax->d.type = AUDIT_EXECVE;
  2082. ax->d.next = context->aux;
  2083. context->aux = (void *)ax;
  2084. return 0;
  2085. }
  2086. /**
  2087. * audit_socketcall - record audit data for sys_socketcall
  2088. * @nargs: number of args
  2089. * @args: args array
  2090. *
  2091. * Returns 0 for success or NULL context or < 0 on error.
  2092. */
  2093. int audit_socketcall(int nargs, unsigned long *args)
  2094. {
  2095. struct audit_aux_data_socketcall *ax;
  2096. struct audit_context *context = current->audit_context;
  2097. if (likely(!context || context->dummy))
  2098. return 0;
  2099. ax = kmalloc(sizeof(*ax) + nargs * sizeof(unsigned long), GFP_KERNEL);
  2100. if (!ax)
  2101. return -ENOMEM;
  2102. ax->nargs = nargs;
  2103. memcpy(ax->args, args, nargs * sizeof(unsigned long));
  2104. ax->d.type = AUDIT_SOCKETCALL;
  2105. ax->d.next = context->aux;
  2106. context->aux = (void *)ax;
  2107. return 0;
  2108. }
  2109. /**
  2110. * __audit_fd_pair - record audit data for pipe and socketpair
  2111. * @fd1: the first file descriptor
  2112. * @fd2: the second file descriptor
  2113. *
  2114. * Returns 0 for success or NULL context or < 0 on error.
  2115. */
  2116. int __audit_fd_pair(int fd1, int fd2)
  2117. {
  2118. struct audit_context *context = current->audit_context;
  2119. struct audit_aux_data_fd_pair *ax;
  2120. if (likely(!context)) {
  2121. return 0;
  2122. }
  2123. ax = kmalloc(sizeof(*ax), GFP_KERNEL);
  2124. if (!ax) {
  2125. return -ENOMEM;
  2126. }
  2127. ax->fd[0] = fd1;
  2128. ax->fd[1] = fd2;
  2129. ax->d.type = AUDIT_FD_PAIR;
  2130. ax->d.next = context->aux;
  2131. context->aux = (void *)ax;
  2132. return 0;
  2133. }
  2134. /**
  2135. * audit_sockaddr - record audit data for sys_bind, sys_connect, sys_sendto
  2136. * @len: data length in user space
  2137. * @a: data address in kernel space
  2138. *
  2139. * Returns 0 for success or NULL context or < 0 on error.
  2140. */
  2141. int audit_sockaddr(int len, void *a)
  2142. {
  2143. struct audit_aux_data_sockaddr *ax;
  2144. struct audit_context *context = current->audit_context;
  2145. if (likely(!context || context->dummy))
  2146. return 0;
  2147. ax = kmalloc(sizeof(*ax) + len, GFP_KERNEL);
  2148. if (!ax)
  2149. return -ENOMEM;
  2150. ax->len = len;
  2151. memcpy(ax->a, a, len);
  2152. ax->d.type = AUDIT_SOCKADDR;
  2153. ax->d.next = context->aux;
  2154. context->aux = (void *)ax;
  2155. return 0;
  2156. }
  2157. void __audit_ptrace(struct task_struct *t)
  2158. {
  2159. struct audit_context *context = current->audit_context;
  2160. context->target_pid = t->pid;
  2161. context->target_auid = audit_get_loginuid(t);
  2162. context->target_uid = t->uid;
  2163. context->target_sessionid = audit_get_sessionid(t);
  2164. security_task_getsecid(t, &context->target_sid);
  2165. memcpy(context->target_comm, t->comm, TASK_COMM_LEN);
  2166. }
  2167. /**
  2168. * audit_signal_info - record signal info for shutting down audit subsystem
  2169. * @sig: signal value
  2170. * @t: task being signaled
  2171. *
  2172. * If the audit subsystem is being terminated, record the task (pid)
  2173. * and uid that is doing that.
  2174. */
  2175. int __audit_signal_info(int sig, struct task_struct *t)
  2176. {
  2177. struct audit_aux_data_pids *axp;
  2178. struct task_struct *tsk = current;
  2179. struct audit_context *ctx = tsk->audit_context;
  2180. if (audit_pid && t->tgid == audit_pid) {
  2181. if (sig == SIGTERM || sig == SIGHUP || sig == SIGUSR1 || sig == SIGUSR2) {
  2182. audit_sig_pid = tsk->pid;
  2183. if (tsk->loginuid != -1)
  2184. audit_sig_uid = tsk->loginuid;
  2185. else
  2186. audit_sig_uid = tsk->uid;
  2187. security_task_getsecid(tsk, &audit_sig_sid);
  2188. }
  2189. if (!audit_signals || audit_dummy_context())
  2190. return 0;
  2191. }
  2192. /* optimize the common case by putting first signal recipient directly
  2193. * in audit_context */
  2194. if (!ctx->target_pid) {
  2195. ctx->target_pid = t->tgid;
  2196. ctx->target_auid = audit_get_loginuid(t);
  2197. ctx->target_uid = t->uid;
  2198. ctx->target_sessionid = audit_get_sessionid(t);
  2199. security_task_getsecid(t, &ctx->target_sid);
  2200. memcpy(ctx->target_comm, t->comm, TASK_COMM_LEN);
  2201. return 0;
  2202. }
  2203. axp = (void *)ctx->aux_pids;
  2204. if (!axp || axp->pid_count == AUDIT_AUX_PIDS) {
  2205. axp = kzalloc(sizeof(*axp), GFP_ATOMIC);
  2206. if (!axp)
  2207. return -ENOMEM;
  2208. axp->d.type = AUDIT_OBJ_PID;
  2209. axp->d.next = ctx->aux_pids;
  2210. ctx->aux_pids = (void *)axp;
  2211. }
  2212. BUG_ON(axp->pid_count >= AUDIT_AUX_PIDS);
  2213. axp->target_pid[axp->pid_count] = t->tgid;
  2214. axp->target_auid[axp->pid_count] = audit_get_loginuid(t);
  2215. axp->target_uid[axp->pid_count] = t->uid;
  2216. axp->target_sessionid[axp->pid_count] = audit_get_sessionid(t);
  2217. security_task_getsecid(t, &axp->target_sid[axp->pid_count]);
  2218. memcpy(axp->target_comm[axp->pid_count], t->comm, TASK_COMM_LEN);
  2219. axp->pid_count++;
  2220. return 0;
  2221. }
  2222. /**
  2223. * audit_core_dumps - record information about processes that end abnormally
  2224. * @signr: signal value
  2225. *
  2226. * If a process ends with a core dump, something fishy is going on and we
  2227. * should record the event for investigation.
  2228. */
  2229. void audit_core_dumps(long signr)
  2230. {
  2231. struct audit_buffer *ab;
  2232. u32 sid;
  2233. uid_t auid = audit_get_loginuid(current);
  2234. unsigned int sessionid = audit_get_sessionid(current);
  2235. if (!audit_enabled)
  2236. return;
  2237. if (signr == SIGQUIT) /* don't care for those */
  2238. return;
  2239. ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_ANOM_ABEND);
  2240. audit_log_format(ab, "auid=%u uid=%u gid=%u ses=%u",
  2241. auid, current->uid, current->gid, sessionid);
  2242. security_task_getsecid(current, &sid);
  2243. if (sid) {
  2244. char *ctx = NULL;
  2245. u32 len;
  2246. if (security_secid_to_secctx(sid, &ctx, &len))
  2247. audit_log_format(ab, " ssid=%u", sid);
  2248. else {
  2249. audit_log_format(ab, " subj=%s", ctx);
  2250. security_release_secctx(ctx, len);
  2251. }
  2252. }
  2253. audit_log_format(ab, " pid=%d comm=", current->pid);
  2254. audit_log_untrustedstring(ab, current->comm);
  2255. audit_log_format(ab, " sig=%ld", signr);
  2256. audit_log_end(ab);
  2257. }