auditsc.c 64 KB

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