auditsc.c 64 KB

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