auditsc.c 64 KB

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