auditsc.c 71 KB

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