smackfs.c 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328
  1. /*
  2. * Copyright (C) 2007 Casey Schaufler <casey@schaufler-ca.com>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, version 2.
  7. *
  8. * Authors:
  9. * Casey Schaufler <casey@schaufler-ca.com>
  10. * Ahmed S. Darwish <darwish.07@gmail.com>
  11. *
  12. * Special thanks to the authors of selinuxfs.
  13. *
  14. * Karl MacMillan <kmacmillan@tresys.com>
  15. * James Morris <jmorris@redhat.com>
  16. *
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/vmalloc.h>
  20. #include <linux/security.h>
  21. #include <linux/mutex.h>
  22. #include <linux/slab.h>
  23. #include <net/net_namespace.h>
  24. #include <net/cipso_ipv4.h>
  25. #include <linux/seq_file.h>
  26. #include <linux/ctype.h>
  27. #include <linux/audit.h>
  28. #include <linux/magic.h>
  29. #include "smack.h"
  30. /*
  31. * smackfs pseudo filesystem.
  32. */
  33. enum smk_inos {
  34. SMK_ROOT_INO = 2,
  35. SMK_LOAD = 3, /* load policy */
  36. SMK_CIPSO = 4, /* load label -> CIPSO mapping */
  37. SMK_DOI = 5, /* CIPSO DOI */
  38. SMK_DIRECT = 6, /* CIPSO level indicating direct label */
  39. SMK_AMBIENT = 7, /* internet ambient label */
  40. SMK_NETLBLADDR = 8, /* single label hosts */
  41. SMK_ONLYCAP = 9, /* the only "capable" label */
  42. SMK_LOGGING = 10, /* logging */
  43. SMK_LOAD_SELF = 11, /* task specific rules */
  44. SMK_ACCESSES = 12, /* access policy */
  45. SMK_MAPPED = 13, /* CIPSO level indicating mapped label */
  46. SMK_LOAD2 = 14, /* load policy with long labels */
  47. SMK_LOAD_SELF2 = 15, /* load task specific rules with long labels */
  48. SMK_ACCESS2 = 16, /* make an access check with long labels */
  49. SMK_CIPSO2 = 17, /* load long label -> CIPSO mapping */
  50. SMK_REVOKE_SUBJ = 18, /* set rules with subject label to '-' */
  51. SMK_CHANGE_RULE = 19, /* change or add rules (long labels) */
  52. };
  53. /*
  54. * List locks
  55. */
  56. static DEFINE_MUTEX(smack_cipso_lock);
  57. static DEFINE_MUTEX(smack_ambient_lock);
  58. static DEFINE_MUTEX(smk_netlbladdr_lock);
  59. /*
  60. * This is the "ambient" label for network traffic.
  61. * If it isn't somehow marked, use this.
  62. * It can be reset via smackfs/ambient
  63. */
  64. char *smack_net_ambient;
  65. /*
  66. * This is the level in a CIPSO header that indicates a
  67. * smack label is contained directly in the category set.
  68. * It can be reset via smackfs/direct
  69. */
  70. int smack_cipso_direct = SMACK_CIPSO_DIRECT_DEFAULT;
  71. /*
  72. * This is the level in a CIPSO header that indicates a
  73. * secid is contained directly in the category set.
  74. * It can be reset via smackfs/mapped
  75. */
  76. int smack_cipso_mapped = SMACK_CIPSO_MAPPED_DEFAULT;
  77. /*
  78. * Unless a process is running with this label even
  79. * having CAP_MAC_OVERRIDE isn't enough to grant
  80. * privilege to violate MAC policy. If no label is
  81. * designated (the NULL case) capabilities apply to
  82. * everyone. It is expected that the hat (^) label
  83. * will be used if any label is used.
  84. */
  85. char *smack_onlycap;
  86. /*
  87. * Certain IP addresses may be designated as single label hosts.
  88. * Packets are sent there unlabeled, but only from tasks that
  89. * can write to the specified label.
  90. */
  91. LIST_HEAD(smk_netlbladdr_list);
  92. /*
  93. * Rule lists are maintained for each label.
  94. * This master list is just for reading /smack/load and /smack/load2.
  95. */
  96. struct smack_master_list {
  97. struct list_head list;
  98. struct smack_rule *smk_rule;
  99. };
  100. LIST_HEAD(smack_rule_list);
  101. struct smack_parsed_rule {
  102. char *smk_subject;
  103. char *smk_object;
  104. int smk_access1;
  105. int smk_access2;
  106. };
  107. static int smk_cipso_doi_value = SMACK_CIPSO_DOI_DEFAULT;
  108. const char *smack_cipso_option = SMACK_CIPSO_OPTION;
  109. /*
  110. * Values for parsing cipso rules
  111. * SMK_DIGITLEN: Length of a digit field in a rule.
  112. * SMK_CIPSOMIN: Minimum possible cipso rule length.
  113. * SMK_CIPSOMAX: Maximum possible cipso rule length.
  114. */
  115. #define SMK_DIGITLEN 4
  116. #define SMK_CIPSOMIN (SMK_LABELLEN + 2 * SMK_DIGITLEN)
  117. #define SMK_CIPSOMAX (SMK_CIPSOMIN + SMACK_CIPSO_MAXCATNUM * SMK_DIGITLEN)
  118. /*
  119. * Values for parsing MAC rules
  120. * SMK_ACCESS: Maximum possible combination of access permissions
  121. * SMK_ACCESSLEN: Maximum length for a rule access field
  122. * SMK_LOADLEN: Smack rule length
  123. */
  124. #define SMK_OACCESS "rwxa"
  125. #define SMK_ACCESS "rwxat"
  126. #define SMK_OACCESSLEN (sizeof(SMK_OACCESS) - 1)
  127. #define SMK_ACCESSLEN (sizeof(SMK_ACCESS) - 1)
  128. #define SMK_OLOADLEN (SMK_LABELLEN + SMK_LABELLEN + SMK_OACCESSLEN)
  129. #define SMK_LOADLEN (SMK_LABELLEN + SMK_LABELLEN + SMK_ACCESSLEN)
  130. /*
  131. * Stricly for CIPSO level manipulation.
  132. * Set the category bit number in a smack label sized buffer.
  133. */
  134. static inline void smack_catset_bit(unsigned int cat, char *catsetp)
  135. {
  136. if (cat == 0 || cat > (SMK_CIPSOLEN * 8))
  137. return;
  138. catsetp[(cat - 1) / 8] |= 0x80 >> ((cat - 1) % 8);
  139. }
  140. /**
  141. * smk_netlabel_audit_set - fill a netlbl_audit struct
  142. * @nap: structure to fill
  143. */
  144. static void smk_netlabel_audit_set(struct netlbl_audit *nap)
  145. {
  146. nap->loginuid = audit_get_loginuid(current);
  147. nap->sessionid = audit_get_sessionid(current);
  148. nap->secid = smack_to_secid(smk_of_current());
  149. }
  150. /*
  151. * Value for parsing single label host rules
  152. * "1.2.3.4 X"
  153. */
  154. #define SMK_NETLBLADDRMIN 9
  155. /**
  156. * smk_set_access - add a rule to the rule list or replace an old rule
  157. * @srp: the rule to add or replace
  158. * @rule_list: the list of rules
  159. * @rule_lock: the rule list lock
  160. * @global: if non-zero, indicates a global rule
  161. *
  162. * Looks through the current subject/object/access list for
  163. * the subject/object pair and replaces the access that was
  164. * there. If the pair isn't found add it with the specified
  165. * access.
  166. *
  167. * Returns 0 if nothing goes wrong or -ENOMEM if it fails
  168. * during the allocation of the new pair to add.
  169. */
  170. static int smk_set_access(struct smack_parsed_rule *srp,
  171. struct list_head *rule_list,
  172. struct mutex *rule_lock, int global)
  173. {
  174. struct smack_rule *sp;
  175. struct smack_master_list *smlp;
  176. int found = 0;
  177. int rc = 0;
  178. mutex_lock(rule_lock);
  179. /*
  180. * Because the object label is less likely to match
  181. * than the subject label check it first
  182. */
  183. list_for_each_entry_rcu(sp, rule_list, list) {
  184. if (sp->smk_object == srp->smk_object &&
  185. sp->smk_subject == srp->smk_subject) {
  186. found = 1;
  187. sp->smk_access |= srp->smk_access1;
  188. sp->smk_access &= ~srp->smk_access2;
  189. break;
  190. }
  191. }
  192. if (found == 0) {
  193. sp = kzalloc(sizeof(*sp), GFP_KERNEL);
  194. if (sp == NULL) {
  195. rc = -ENOMEM;
  196. goto out;
  197. }
  198. sp->smk_subject = srp->smk_subject;
  199. sp->smk_object = srp->smk_object;
  200. sp->smk_access = srp->smk_access1 & ~srp->smk_access2;
  201. list_add_rcu(&sp->list, rule_list);
  202. /*
  203. * If this is a global as opposed to self and a new rule
  204. * it needs to get added for reporting.
  205. */
  206. if (global) {
  207. smlp = kzalloc(sizeof(*smlp), GFP_KERNEL);
  208. if (smlp != NULL) {
  209. smlp->smk_rule = sp;
  210. list_add_rcu(&smlp->list, &smack_rule_list);
  211. } else
  212. rc = -ENOMEM;
  213. }
  214. }
  215. out:
  216. mutex_unlock(rule_lock);
  217. return rc;
  218. }
  219. /**
  220. * smk_perm_from_str - parse smack accesses from a text string
  221. * @string: a text string that contains a Smack accesses code
  222. *
  223. * Returns an integer with respective bits set for specified accesses.
  224. */
  225. static int smk_perm_from_str(const char *string)
  226. {
  227. int perm = 0;
  228. const char *cp;
  229. for (cp = string; ; cp++)
  230. switch (*cp) {
  231. case '-':
  232. break;
  233. case 'r':
  234. case 'R':
  235. perm |= MAY_READ;
  236. break;
  237. case 'w':
  238. case 'W':
  239. perm |= MAY_WRITE;
  240. break;
  241. case 'x':
  242. case 'X':
  243. perm |= MAY_EXEC;
  244. break;
  245. case 'a':
  246. case 'A':
  247. perm |= MAY_APPEND;
  248. break;
  249. case 't':
  250. case 'T':
  251. perm |= MAY_TRANSMUTE;
  252. break;
  253. default:
  254. return perm;
  255. }
  256. }
  257. /**
  258. * smk_fill_rule - Fill Smack rule from strings
  259. * @subject: subject label string
  260. * @object: object label string
  261. * @access1: access string
  262. * @access2: string with permissions to be removed
  263. * @rule: Smack rule
  264. * @import: if non-zero, import labels
  265. * @len: label length limit
  266. *
  267. * Returns 0 on success, -1 on failure
  268. */
  269. static int smk_fill_rule(const char *subject, const char *object,
  270. const char *access1, const char *access2,
  271. struct smack_parsed_rule *rule, int import,
  272. int len)
  273. {
  274. const char *cp;
  275. struct smack_known *skp;
  276. if (import) {
  277. rule->smk_subject = smk_import(subject, len);
  278. if (rule->smk_subject == NULL)
  279. return -1;
  280. rule->smk_object = smk_import(object, len);
  281. if (rule->smk_object == NULL)
  282. return -1;
  283. } else {
  284. cp = smk_parse_smack(subject, len);
  285. if (cp == NULL)
  286. return -1;
  287. skp = smk_find_entry(cp);
  288. kfree(cp);
  289. if (skp == NULL)
  290. return -1;
  291. rule->smk_subject = skp->smk_known;
  292. cp = smk_parse_smack(object, len);
  293. if (cp == NULL)
  294. return -1;
  295. skp = smk_find_entry(cp);
  296. kfree(cp);
  297. if (skp == NULL)
  298. return -1;
  299. rule->smk_object = skp->smk_known;
  300. }
  301. rule->smk_access1 = smk_perm_from_str(access1);
  302. if (access2)
  303. rule->smk_access2 = smk_perm_from_str(access2);
  304. else
  305. rule->smk_access2 = ~rule->smk_access1;
  306. return 0;
  307. }
  308. /**
  309. * smk_parse_rule - parse Smack rule from load string
  310. * @data: string to be parsed whose size is SMK_LOADLEN
  311. * @rule: Smack rule
  312. * @import: if non-zero, import labels
  313. *
  314. * Returns 0 on success, -1 on errors.
  315. */
  316. static int smk_parse_rule(const char *data, struct smack_parsed_rule *rule,
  317. int import)
  318. {
  319. int rc;
  320. rc = smk_fill_rule(data, data + SMK_LABELLEN,
  321. data + SMK_LABELLEN + SMK_LABELLEN, NULL, rule,
  322. import, SMK_LABELLEN);
  323. return rc;
  324. }
  325. /**
  326. * smk_parse_long_rule - parse Smack rule from rule string
  327. * @data: string to be parsed, null terminated
  328. * @rule: Will be filled with Smack parsed rule
  329. * @import: if non-zero, import labels
  330. * @change: if non-zero, data is from /smack/change-rule
  331. *
  332. * Returns 0 on success, -1 on failure
  333. */
  334. static int smk_parse_long_rule(const char *data, struct smack_parsed_rule *rule,
  335. int import, int change)
  336. {
  337. char *subject;
  338. char *object;
  339. char *access1;
  340. char *access2;
  341. int datalen;
  342. int rc = -1;
  343. /* This is inefficient */
  344. datalen = strlen(data);
  345. /* Our first element can be 64 + \0 with no spaces */
  346. subject = kzalloc(datalen + 1, GFP_KERNEL);
  347. if (subject == NULL)
  348. return -1;
  349. object = kzalloc(datalen, GFP_KERNEL);
  350. if (object == NULL)
  351. goto free_out_s;
  352. access1 = kzalloc(datalen, GFP_KERNEL);
  353. if (access1 == NULL)
  354. goto free_out_o;
  355. access2 = kzalloc(datalen, GFP_KERNEL);
  356. if (access2 == NULL)
  357. goto free_out_a;
  358. if (change) {
  359. if (sscanf(data, "%s %s %s %s",
  360. subject, object, access1, access2) == 4)
  361. rc = smk_fill_rule(subject, object, access1, access2,
  362. rule, import, 0);
  363. } else {
  364. if (sscanf(data, "%s %s %s", subject, object, access1) == 3)
  365. rc = smk_fill_rule(subject, object, access1, NULL,
  366. rule, import, 0);
  367. }
  368. kfree(access2);
  369. free_out_a:
  370. kfree(access1);
  371. free_out_o:
  372. kfree(object);
  373. free_out_s:
  374. kfree(subject);
  375. return rc;
  376. }
  377. #define SMK_FIXED24_FMT 0 /* Fixed 24byte label format */
  378. #define SMK_LONG_FMT 1 /* Variable long label format */
  379. #define SMK_CHANGE_FMT 2 /* Rule modification format */
  380. /**
  381. * smk_write_rules_list - write() for any /smack rule file
  382. * @file: file pointer, not actually used
  383. * @buf: where to get the data from
  384. * @count: bytes sent
  385. * @ppos: where to start - must be 0
  386. * @rule_list: the list of rules to write to
  387. * @rule_lock: lock for the rule list
  388. * @format: /smack/load or /smack/load2 or /smack/change-rule format.
  389. *
  390. * Get one smack access rule from above.
  391. * The format for SMK_LONG_FMT is:
  392. * "subject<whitespace>object<whitespace>access[<whitespace>...]"
  393. * The format for SMK_FIXED24_FMT is exactly:
  394. * "subject object rwxat"
  395. * The format for SMK_CHANGE_FMT is:
  396. * "subject<whitespace>object<whitespace>
  397. * acc_enable<whitespace>acc_disable[<whitespace>...]"
  398. */
  399. static ssize_t smk_write_rules_list(struct file *file, const char __user *buf,
  400. size_t count, loff_t *ppos,
  401. struct list_head *rule_list,
  402. struct mutex *rule_lock, int format)
  403. {
  404. struct smack_known *skp;
  405. struct smack_parsed_rule *rule;
  406. char *data;
  407. int datalen;
  408. int rc = -EINVAL;
  409. int load = 0;
  410. /*
  411. * No partial writes.
  412. * Enough data must be present.
  413. */
  414. if (*ppos != 0)
  415. return -EINVAL;
  416. if (format == SMK_FIXED24_FMT) {
  417. /*
  418. * Minor hack for backward compatibility
  419. */
  420. if (count != SMK_OLOADLEN && count != SMK_LOADLEN)
  421. return -EINVAL;
  422. datalen = SMK_LOADLEN;
  423. } else
  424. datalen = count + 1;
  425. data = kzalloc(datalen, GFP_KERNEL);
  426. if (data == NULL)
  427. return -ENOMEM;
  428. if (copy_from_user(data, buf, count) != 0) {
  429. rc = -EFAULT;
  430. goto out;
  431. }
  432. rule = kzalloc(sizeof(*rule), GFP_KERNEL);
  433. if (rule == NULL) {
  434. rc = -ENOMEM;
  435. goto out;
  436. }
  437. if (format == SMK_LONG_FMT) {
  438. /*
  439. * Be sure the data string is terminated.
  440. */
  441. data[count] = '\0';
  442. if (smk_parse_long_rule(data, rule, 1, 0))
  443. goto out_free_rule;
  444. } else if (format == SMK_CHANGE_FMT) {
  445. data[count] = '\0';
  446. if (smk_parse_long_rule(data, rule, 1, 1))
  447. goto out_free_rule;
  448. } else {
  449. /*
  450. * More on the minor hack for backward compatibility
  451. */
  452. if (count == (SMK_OLOADLEN))
  453. data[SMK_OLOADLEN] = '-';
  454. if (smk_parse_rule(data, rule, 1))
  455. goto out_free_rule;
  456. }
  457. if (rule_list == NULL) {
  458. load = 1;
  459. skp = smk_find_entry(rule->smk_subject);
  460. rule_list = &skp->smk_rules;
  461. rule_lock = &skp->smk_rules_lock;
  462. }
  463. rc = smk_set_access(rule, rule_list, rule_lock, load);
  464. if (rc == 0) {
  465. rc = count;
  466. goto out;
  467. }
  468. out_free_rule:
  469. kfree(rule);
  470. out:
  471. kfree(data);
  472. return rc;
  473. }
  474. /*
  475. * Core logic for smackfs seq list operations.
  476. */
  477. static void *smk_seq_start(struct seq_file *s, loff_t *pos,
  478. struct list_head *head)
  479. {
  480. struct list_head *list;
  481. /*
  482. * This is 0 the first time through.
  483. */
  484. if (s->index == 0)
  485. s->private = head;
  486. if (s->private == NULL)
  487. return NULL;
  488. list = s->private;
  489. if (list_empty(list))
  490. return NULL;
  491. if (s->index == 0)
  492. return list->next;
  493. return list;
  494. }
  495. static void *smk_seq_next(struct seq_file *s, void *v, loff_t *pos,
  496. struct list_head *head)
  497. {
  498. struct list_head *list = v;
  499. if (list_is_last(list, head)) {
  500. s->private = NULL;
  501. return NULL;
  502. }
  503. s->private = list->next;
  504. return list->next;
  505. }
  506. static void smk_seq_stop(struct seq_file *s, void *v)
  507. {
  508. /* No-op */
  509. }
  510. static void smk_rule_show(struct seq_file *s, struct smack_rule *srp, int max)
  511. {
  512. /*
  513. * Don't show any rules with label names too long for
  514. * interface file (/smack/load or /smack/load2)
  515. * because you should expect to be able to write
  516. * anything you read back.
  517. */
  518. if (strlen(srp->smk_subject) >= max || strlen(srp->smk_object) >= max)
  519. return;
  520. if (srp->smk_access == 0)
  521. return;
  522. seq_printf(s, "%s %s", srp->smk_subject, srp->smk_object);
  523. seq_putc(s, ' ');
  524. if (srp->smk_access & MAY_READ)
  525. seq_putc(s, 'r');
  526. if (srp->smk_access & MAY_WRITE)
  527. seq_putc(s, 'w');
  528. if (srp->smk_access & MAY_EXEC)
  529. seq_putc(s, 'x');
  530. if (srp->smk_access & MAY_APPEND)
  531. seq_putc(s, 'a');
  532. if (srp->smk_access & MAY_TRANSMUTE)
  533. seq_putc(s, 't');
  534. seq_putc(s, '\n');
  535. }
  536. /*
  537. * Seq_file read operations for /smack/load
  538. */
  539. static void *load2_seq_start(struct seq_file *s, loff_t *pos)
  540. {
  541. return smk_seq_start(s, pos, &smack_rule_list);
  542. }
  543. static void *load2_seq_next(struct seq_file *s, void *v, loff_t *pos)
  544. {
  545. return smk_seq_next(s, v, pos, &smack_rule_list);
  546. }
  547. static int load_seq_show(struct seq_file *s, void *v)
  548. {
  549. struct list_head *list = v;
  550. struct smack_master_list *smlp =
  551. list_entry(list, struct smack_master_list, list);
  552. smk_rule_show(s, smlp->smk_rule, SMK_LABELLEN);
  553. return 0;
  554. }
  555. static const struct seq_operations load_seq_ops = {
  556. .start = load2_seq_start,
  557. .next = load2_seq_next,
  558. .show = load_seq_show,
  559. .stop = smk_seq_stop,
  560. };
  561. /**
  562. * smk_open_load - open() for /smack/load
  563. * @inode: inode structure representing file
  564. * @file: "load" file pointer
  565. *
  566. * For reading, use load_seq_* seq_file reading operations.
  567. */
  568. static int smk_open_load(struct inode *inode, struct file *file)
  569. {
  570. return seq_open(file, &load_seq_ops);
  571. }
  572. /**
  573. * smk_write_load - write() for /smack/load
  574. * @file: file pointer, not actually used
  575. * @buf: where to get the data from
  576. * @count: bytes sent
  577. * @ppos: where to start - must be 0
  578. *
  579. */
  580. static ssize_t smk_write_load(struct file *file, const char __user *buf,
  581. size_t count, loff_t *ppos)
  582. {
  583. /*
  584. * Must have privilege.
  585. * No partial writes.
  586. * Enough data must be present.
  587. */
  588. if (!smack_privileged(CAP_MAC_ADMIN))
  589. return -EPERM;
  590. return smk_write_rules_list(file, buf, count, ppos, NULL, NULL,
  591. SMK_FIXED24_FMT);
  592. }
  593. static const struct file_operations smk_load_ops = {
  594. .open = smk_open_load,
  595. .read = seq_read,
  596. .llseek = seq_lseek,
  597. .write = smk_write_load,
  598. .release = seq_release,
  599. };
  600. /**
  601. * smk_cipso_doi - initialize the CIPSO domain
  602. */
  603. static void smk_cipso_doi(void)
  604. {
  605. int rc;
  606. struct cipso_v4_doi *doip;
  607. struct netlbl_audit nai;
  608. smk_netlabel_audit_set(&nai);
  609. rc = netlbl_cfg_map_del(NULL, PF_INET, NULL, NULL, &nai);
  610. if (rc != 0)
  611. printk(KERN_WARNING "%s:%d remove rc = %d\n",
  612. __func__, __LINE__, rc);
  613. doip = kmalloc(sizeof(struct cipso_v4_doi), GFP_KERNEL);
  614. if (doip == NULL)
  615. panic("smack: Failed to initialize cipso DOI.\n");
  616. doip->map.std = NULL;
  617. doip->doi = smk_cipso_doi_value;
  618. doip->type = CIPSO_V4_MAP_PASS;
  619. doip->tags[0] = CIPSO_V4_TAG_RBITMAP;
  620. for (rc = 1; rc < CIPSO_V4_TAG_MAXCNT; rc++)
  621. doip->tags[rc] = CIPSO_V4_TAG_INVALID;
  622. rc = netlbl_cfg_cipsov4_add(doip, &nai);
  623. if (rc != 0) {
  624. printk(KERN_WARNING "%s:%d cipso add rc = %d\n",
  625. __func__, __LINE__, rc);
  626. kfree(doip);
  627. return;
  628. }
  629. rc = netlbl_cfg_cipsov4_map_add(doip->doi, NULL, NULL, NULL, &nai);
  630. if (rc != 0) {
  631. printk(KERN_WARNING "%s:%d map add rc = %d\n",
  632. __func__, __LINE__, rc);
  633. kfree(doip);
  634. return;
  635. }
  636. }
  637. /**
  638. * smk_unlbl_ambient - initialize the unlabeled domain
  639. * @oldambient: previous domain string
  640. */
  641. static void smk_unlbl_ambient(char *oldambient)
  642. {
  643. int rc;
  644. struct netlbl_audit nai;
  645. smk_netlabel_audit_set(&nai);
  646. if (oldambient != NULL) {
  647. rc = netlbl_cfg_map_del(oldambient, PF_INET, NULL, NULL, &nai);
  648. if (rc != 0)
  649. printk(KERN_WARNING "%s:%d remove rc = %d\n",
  650. __func__, __LINE__, rc);
  651. }
  652. if (smack_net_ambient == NULL)
  653. smack_net_ambient = smack_known_floor.smk_known;
  654. rc = netlbl_cfg_unlbl_map_add(smack_net_ambient, PF_INET,
  655. NULL, NULL, &nai);
  656. if (rc != 0)
  657. printk(KERN_WARNING "%s:%d add rc = %d\n",
  658. __func__, __LINE__, rc);
  659. }
  660. /*
  661. * Seq_file read operations for /smack/cipso
  662. */
  663. static void *cipso_seq_start(struct seq_file *s, loff_t *pos)
  664. {
  665. return smk_seq_start(s, pos, &smack_known_list);
  666. }
  667. static void *cipso_seq_next(struct seq_file *s, void *v, loff_t *pos)
  668. {
  669. return smk_seq_next(s, v, pos, &smack_known_list);
  670. }
  671. /*
  672. * Print cipso labels in format:
  673. * label level[/cat[,cat]]
  674. */
  675. static int cipso_seq_show(struct seq_file *s, void *v)
  676. {
  677. struct list_head *list = v;
  678. struct smack_known *skp =
  679. list_entry(list, struct smack_known, list);
  680. struct netlbl_lsm_secattr_catmap *cmp = skp->smk_netlabel.attr.mls.cat;
  681. char sep = '/';
  682. int i;
  683. /*
  684. * Don't show a label that could not have been set using
  685. * /smack/cipso. This is in support of the notion that
  686. * anything read from /smack/cipso ought to be writeable
  687. * to /smack/cipso.
  688. *
  689. * /smack/cipso2 should be used instead.
  690. */
  691. if (strlen(skp->smk_known) >= SMK_LABELLEN)
  692. return 0;
  693. seq_printf(s, "%s %3d", skp->smk_known, skp->smk_netlabel.attr.mls.lvl);
  694. for (i = netlbl_secattr_catmap_walk(cmp, 0); i >= 0;
  695. i = netlbl_secattr_catmap_walk(cmp, i + 1)) {
  696. seq_printf(s, "%c%d", sep, i);
  697. sep = ',';
  698. }
  699. seq_putc(s, '\n');
  700. return 0;
  701. }
  702. static const struct seq_operations cipso_seq_ops = {
  703. .start = cipso_seq_start,
  704. .next = cipso_seq_next,
  705. .show = cipso_seq_show,
  706. .stop = smk_seq_stop,
  707. };
  708. /**
  709. * smk_open_cipso - open() for /smack/cipso
  710. * @inode: inode structure representing file
  711. * @file: "cipso" file pointer
  712. *
  713. * Connect our cipso_seq_* operations with /smack/cipso
  714. * file_operations
  715. */
  716. static int smk_open_cipso(struct inode *inode, struct file *file)
  717. {
  718. return seq_open(file, &cipso_seq_ops);
  719. }
  720. /**
  721. * smk_set_cipso - do the work for write() for cipso and cipso2
  722. * @file: file pointer, not actually used
  723. * @buf: where to get the data from
  724. * @count: bytes sent
  725. * @ppos: where to start
  726. * @format: /smack/cipso or /smack/cipso2
  727. *
  728. * Accepts only one cipso rule per write call.
  729. * Returns number of bytes written or error code, as appropriate
  730. */
  731. static ssize_t smk_set_cipso(struct file *file, const char __user *buf,
  732. size_t count, loff_t *ppos, int format)
  733. {
  734. struct smack_known *skp;
  735. struct netlbl_lsm_secattr ncats;
  736. char mapcatset[SMK_CIPSOLEN];
  737. int maplevel;
  738. unsigned int cat;
  739. int catlen;
  740. ssize_t rc = -EINVAL;
  741. char *data = NULL;
  742. char *rule;
  743. int ret;
  744. int i;
  745. /*
  746. * Must have privilege.
  747. * No partial writes.
  748. * Enough data must be present.
  749. */
  750. if (!smack_privileged(CAP_MAC_ADMIN))
  751. return -EPERM;
  752. if (*ppos != 0)
  753. return -EINVAL;
  754. if (format == SMK_FIXED24_FMT &&
  755. (count < SMK_CIPSOMIN || count > SMK_CIPSOMAX))
  756. return -EINVAL;
  757. data = kzalloc(count + 1, GFP_KERNEL);
  758. if (data == NULL)
  759. return -ENOMEM;
  760. if (copy_from_user(data, buf, count) != 0) {
  761. rc = -EFAULT;
  762. goto unlockedout;
  763. }
  764. data[count] = '\0';
  765. rule = data;
  766. /*
  767. * Only allow one writer at a time. Writes should be
  768. * quite rare and small in any case.
  769. */
  770. mutex_lock(&smack_cipso_lock);
  771. skp = smk_import_entry(rule, 0);
  772. if (skp == NULL)
  773. goto out;
  774. if (format == SMK_FIXED24_FMT)
  775. rule += SMK_LABELLEN;
  776. else
  777. rule += strlen(skp->smk_known);
  778. ret = sscanf(rule, "%d", &maplevel);
  779. if (ret != 1 || maplevel > SMACK_CIPSO_MAXLEVEL)
  780. goto out;
  781. rule += SMK_DIGITLEN;
  782. ret = sscanf(rule, "%d", &catlen);
  783. if (ret != 1 || catlen > SMACK_CIPSO_MAXCATNUM)
  784. goto out;
  785. if (format == SMK_FIXED24_FMT &&
  786. count != (SMK_CIPSOMIN + catlen * SMK_DIGITLEN))
  787. goto out;
  788. memset(mapcatset, 0, sizeof(mapcatset));
  789. for (i = 0; i < catlen; i++) {
  790. rule += SMK_DIGITLEN;
  791. ret = sscanf(rule, "%u", &cat);
  792. if (ret != 1 || cat > SMACK_CIPSO_MAXCATVAL)
  793. goto out;
  794. smack_catset_bit(cat, mapcatset);
  795. }
  796. rc = smk_netlbl_mls(maplevel, mapcatset, &ncats, SMK_CIPSOLEN);
  797. if (rc >= 0) {
  798. netlbl_secattr_catmap_free(skp->smk_netlabel.attr.mls.cat);
  799. skp->smk_netlabel.attr.mls.cat = ncats.attr.mls.cat;
  800. skp->smk_netlabel.attr.mls.lvl = ncats.attr.mls.lvl;
  801. rc = count;
  802. }
  803. out:
  804. mutex_unlock(&smack_cipso_lock);
  805. unlockedout:
  806. kfree(data);
  807. return rc;
  808. }
  809. /**
  810. * smk_write_cipso - write() for /smack/cipso
  811. * @file: file pointer, not actually used
  812. * @buf: where to get the data from
  813. * @count: bytes sent
  814. * @ppos: where to start
  815. *
  816. * Accepts only one cipso rule per write call.
  817. * Returns number of bytes written or error code, as appropriate
  818. */
  819. static ssize_t smk_write_cipso(struct file *file, const char __user *buf,
  820. size_t count, loff_t *ppos)
  821. {
  822. return smk_set_cipso(file, buf, count, ppos, SMK_FIXED24_FMT);
  823. }
  824. static const struct file_operations smk_cipso_ops = {
  825. .open = smk_open_cipso,
  826. .read = seq_read,
  827. .llseek = seq_lseek,
  828. .write = smk_write_cipso,
  829. .release = seq_release,
  830. };
  831. /*
  832. * Seq_file read operations for /smack/cipso2
  833. */
  834. /*
  835. * Print cipso labels in format:
  836. * label level[/cat[,cat]]
  837. */
  838. static int cipso2_seq_show(struct seq_file *s, void *v)
  839. {
  840. struct list_head *list = v;
  841. struct smack_known *skp =
  842. list_entry(list, struct smack_known, list);
  843. struct netlbl_lsm_secattr_catmap *cmp = skp->smk_netlabel.attr.mls.cat;
  844. char sep = '/';
  845. int i;
  846. seq_printf(s, "%s %3d", skp->smk_known, skp->smk_netlabel.attr.mls.lvl);
  847. for (i = netlbl_secattr_catmap_walk(cmp, 0); i >= 0;
  848. i = netlbl_secattr_catmap_walk(cmp, i + 1)) {
  849. seq_printf(s, "%c%d", sep, i);
  850. sep = ',';
  851. }
  852. seq_putc(s, '\n');
  853. return 0;
  854. }
  855. static const struct seq_operations cipso2_seq_ops = {
  856. .start = cipso_seq_start,
  857. .next = cipso_seq_next,
  858. .show = cipso2_seq_show,
  859. .stop = smk_seq_stop,
  860. };
  861. /**
  862. * smk_open_cipso2 - open() for /smack/cipso2
  863. * @inode: inode structure representing file
  864. * @file: "cipso2" file pointer
  865. *
  866. * Connect our cipso_seq_* operations with /smack/cipso2
  867. * file_operations
  868. */
  869. static int smk_open_cipso2(struct inode *inode, struct file *file)
  870. {
  871. return seq_open(file, &cipso2_seq_ops);
  872. }
  873. /**
  874. * smk_write_cipso2 - write() for /smack/cipso2
  875. * @file: file pointer, not actually used
  876. * @buf: where to get the data from
  877. * @count: bytes sent
  878. * @ppos: where to start
  879. *
  880. * Accepts only one cipso rule per write call.
  881. * Returns number of bytes written or error code, as appropriate
  882. */
  883. static ssize_t smk_write_cipso2(struct file *file, const char __user *buf,
  884. size_t count, loff_t *ppos)
  885. {
  886. return smk_set_cipso(file, buf, count, ppos, SMK_LONG_FMT);
  887. }
  888. static const struct file_operations smk_cipso2_ops = {
  889. .open = smk_open_cipso2,
  890. .read = seq_read,
  891. .llseek = seq_lseek,
  892. .write = smk_write_cipso2,
  893. .release = seq_release,
  894. };
  895. /*
  896. * Seq_file read operations for /smack/netlabel
  897. */
  898. static void *netlbladdr_seq_start(struct seq_file *s, loff_t *pos)
  899. {
  900. return smk_seq_start(s, pos, &smk_netlbladdr_list);
  901. }
  902. static void *netlbladdr_seq_next(struct seq_file *s, void *v, loff_t *pos)
  903. {
  904. return smk_seq_next(s, v, pos, &smk_netlbladdr_list);
  905. }
  906. #define BEBITS (sizeof(__be32) * 8)
  907. /*
  908. * Print host/label pairs
  909. */
  910. static int netlbladdr_seq_show(struct seq_file *s, void *v)
  911. {
  912. struct list_head *list = v;
  913. struct smk_netlbladdr *skp =
  914. list_entry(list, struct smk_netlbladdr, list);
  915. unsigned char *hp = (char *) &skp->smk_host.sin_addr.s_addr;
  916. int maskn;
  917. u32 temp_mask = be32_to_cpu(skp->smk_mask.s_addr);
  918. for (maskn = 0; temp_mask; temp_mask <<= 1, maskn++);
  919. seq_printf(s, "%u.%u.%u.%u/%d %s\n",
  920. hp[0], hp[1], hp[2], hp[3], maskn, skp->smk_label);
  921. return 0;
  922. }
  923. static const struct seq_operations netlbladdr_seq_ops = {
  924. .start = netlbladdr_seq_start,
  925. .next = netlbladdr_seq_next,
  926. .show = netlbladdr_seq_show,
  927. .stop = smk_seq_stop,
  928. };
  929. /**
  930. * smk_open_netlbladdr - open() for /smack/netlabel
  931. * @inode: inode structure representing file
  932. * @file: "netlabel" file pointer
  933. *
  934. * Connect our netlbladdr_seq_* operations with /smack/netlabel
  935. * file_operations
  936. */
  937. static int smk_open_netlbladdr(struct inode *inode, struct file *file)
  938. {
  939. return seq_open(file, &netlbladdr_seq_ops);
  940. }
  941. /**
  942. * smk_netlbladdr_insert
  943. * @new : netlabel to insert
  944. *
  945. * This helper insert netlabel in the smack_netlbladdrs list
  946. * sorted by netmask length (longest to smallest)
  947. * locked by &smk_netlbladdr_lock in smk_write_netlbladdr
  948. *
  949. */
  950. static void smk_netlbladdr_insert(struct smk_netlbladdr *new)
  951. {
  952. struct smk_netlbladdr *m, *m_next;
  953. if (list_empty(&smk_netlbladdr_list)) {
  954. list_add_rcu(&new->list, &smk_netlbladdr_list);
  955. return;
  956. }
  957. m = list_entry_rcu(smk_netlbladdr_list.next,
  958. struct smk_netlbladdr, list);
  959. /* the comparison '>' is a bit hacky, but works */
  960. if (new->smk_mask.s_addr > m->smk_mask.s_addr) {
  961. list_add_rcu(&new->list, &smk_netlbladdr_list);
  962. return;
  963. }
  964. list_for_each_entry_rcu(m, &smk_netlbladdr_list, list) {
  965. if (list_is_last(&m->list, &smk_netlbladdr_list)) {
  966. list_add_rcu(&new->list, &m->list);
  967. return;
  968. }
  969. m_next = list_entry_rcu(m->list.next,
  970. struct smk_netlbladdr, list);
  971. if (new->smk_mask.s_addr > m_next->smk_mask.s_addr) {
  972. list_add_rcu(&new->list, &m->list);
  973. return;
  974. }
  975. }
  976. }
  977. /**
  978. * smk_write_netlbladdr - write() for /smack/netlabel
  979. * @file: file pointer, not actually used
  980. * @buf: where to get the data from
  981. * @count: bytes sent
  982. * @ppos: where to start
  983. *
  984. * Accepts only one netlbladdr per write call.
  985. * Returns number of bytes written or error code, as appropriate
  986. */
  987. static ssize_t smk_write_netlbladdr(struct file *file, const char __user *buf,
  988. size_t count, loff_t *ppos)
  989. {
  990. struct smk_netlbladdr *skp;
  991. struct sockaddr_in newname;
  992. char *smack;
  993. char *sp;
  994. char *data;
  995. char *host = (char *)&newname.sin_addr.s_addr;
  996. int rc;
  997. struct netlbl_audit audit_info;
  998. struct in_addr mask;
  999. unsigned int m;
  1000. int found;
  1001. u32 mask_bits = (1<<31);
  1002. __be32 nsa;
  1003. u32 temp_mask;
  1004. /*
  1005. * Must have privilege.
  1006. * No partial writes.
  1007. * Enough data must be present.
  1008. * "<addr/mask, as a.b.c.d/e><space><label>"
  1009. * "<addr, as a.b.c.d><space><label>"
  1010. */
  1011. if (!smack_privileged(CAP_MAC_ADMIN))
  1012. return -EPERM;
  1013. if (*ppos != 0)
  1014. return -EINVAL;
  1015. if (count < SMK_NETLBLADDRMIN)
  1016. return -EINVAL;
  1017. data = kzalloc(count + 1, GFP_KERNEL);
  1018. if (data == NULL)
  1019. return -ENOMEM;
  1020. if (copy_from_user(data, buf, count) != 0) {
  1021. rc = -EFAULT;
  1022. goto free_data_out;
  1023. }
  1024. smack = kzalloc(count + 1, GFP_KERNEL);
  1025. if (smack == NULL) {
  1026. rc = -ENOMEM;
  1027. goto free_data_out;
  1028. }
  1029. data[count] = '\0';
  1030. rc = sscanf(data, "%hhd.%hhd.%hhd.%hhd/%d %s",
  1031. &host[0], &host[1], &host[2], &host[3], &m, smack);
  1032. if (rc != 6) {
  1033. rc = sscanf(data, "%hhd.%hhd.%hhd.%hhd %s",
  1034. &host[0], &host[1], &host[2], &host[3], smack);
  1035. if (rc != 5) {
  1036. rc = -EINVAL;
  1037. goto free_out;
  1038. }
  1039. m = BEBITS;
  1040. }
  1041. if (m > BEBITS) {
  1042. rc = -EINVAL;
  1043. goto free_out;
  1044. }
  1045. /*
  1046. * If smack begins with '-', it is an option, don't import it
  1047. */
  1048. if (smack[0] != '-') {
  1049. sp = smk_import(smack, 0);
  1050. if (sp == NULL) {
  1051. rc = -EINVAL;
  1052. goto free_out;
  1053. }
  1054. } else {
  1055. /* check known options */
  1056. if (strcmp(smack, smack_cipso_option) == 0)
  1057. sp = (char *)smack_cipso_option;
  1058. else {
  1059. rc = -EINVAL;
  1060. goto free_out;
  1061. }
  1062. }
  1063. for (temp_mask = 0; m > 0; m--) {
  1064. temp_mask |= mask_bits;
  1065. mask_bits >>= 1;
  1066. }
  1067. mask.s_addr = cpu_to_be32(temp_mask);
  1068. newname.sin_addr.s_addr &= mask.s_addr;
  1069. /*
  1070. * Only allow one writer at a time. Writes should be
  1071. * quite rare and small in any case.
  1072. */
  1073. mutex_lock(&smk_netlbladdr_lock);
  1074. nsa = newname.sin_addr.s_addr;
  1075. /* try to find if the prefix is already in the list */
  1076. found = 0;
  1077. list_for_each_entry_rcu(skp, &smk_netlbladdr_list, list) {
  1078. if (skp->smk_host.sin_addr.s_addr == nsa &&
  1079. skp->smk_mask.s_addr == mask.s_addr) {
  1080. found = 1;
  1081. break;
  1082. }
  1083. }
  1084. smk_netlabel_audit_set(&audit_info);
  1085. if (found == 0) {
  1086. skp = kzalloc(sizeof(*skp), GFP_KERNEL);
  1087. if (skp == NULL)
  1088. rc = -ENOMEM;
  1089. else {
  1090. rc = 0;
  1091. skp->smk_host.sin_addr.s_addr = newname.sin_addr.s_addr;
  1092. skp->smk_mask.s_addr = mask.s_addr;
  1093. skp->smk_label = sp;
  1094. smk_netlbladdr_insert(skp);
  1095. }
  1096. } else {
  1097. /* we delete the unlabeled entry, only if the previous label
  1098. * wasn't the special CIPSO option */
  1099. if (skp->smk_label != smack_cipso_option)
  1100. rc = netlbl_cfg_unlbl_static_del(&init_net, NULL,
  1101. &skp->smk_host.sin_addr, &skp->smk_mask,
  1102. PF_INET, &audit_info);
  1103. else
  1104. rc = 0;
  1105. skp->smk_label = sp;
  1106. }
  1107. /*
  1108. * Now tell netlabel about the single label nature of
  1109. * this host so that incoming packets get labeled.
  1110. * but only if we didn't get the special CIPSO option
  1111. */
  1112. if (rc == 0 && sp != smack_cipso_option)
  1113. rc = netlbl_cfg_unlbl_static_add(&init_net, NULL,
  1114. &skp->smk_host.sin_addr, &skp->smk_mask, PF_INET,
  1115. smack_to_secid(skp->smk_label), &audit_info);
  1116. if (rc == 0)
  1117. rc = count;
  1118. mutex_unlock(&smk_netlbladdr_lock);
  1119. free_out:
  1120. kfree(smack);
  1121. free_data_out:
  1122. kfree(data);
  1123. return rc;
  1124. }
  1125. static const struct file_operations smk_netlbladdr_ops = {
  1126. .open = smk_open_netlbladdr,
  1127. .read = seq_read,
  1128. .llseek = seq_lseek,
  1129. .write = smk_write_netlbladdr,
  1130. .release = seq_release,
  1131. };
  1132. /**
  1133. * smk_read_doi - read() for /smack/doi
  1134. * @filp: file pointer, not actually used
  1135. * @buf: where to put the result
  1136. * @count: maximum to send along
  1137. * @ppos: where to start
  1138. *
  1139. * Returns number of bytes read or error code, as appropriate
  1140. */
  1141. static ssize_t smk_read_doi(struct file *filp, char __user *buf,
  1142. size_t count, loff_t *ppos)
  1143. {
  1144. char temp[80];
  1145. ssize_t rc;
  1146. if (*ppos != 0)
  1147. return 0;
  1148. sprintf(temp, "%d", smk_cipso_doi_value);
  1149. rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
  1150. return rc;
  1151. }
  1152. /**
  1153. * smk_write_doi - write() for /smack/doi
  1154. * @file: file pointer, not actually used
  1155. * @buf: where to get the data from
  1156. * @count: bytes sent
  1157. * @ppos: where to start
  1158. *
  1159. * Returns number of bytes written or error code, as appropriate
  1160. */
  1161. static ssize_t smk_write_doi(struct file *file, const char __user *buf,
  1162. size_t count, loff_t *ppos)
  1163. {
  1164. char temp[80];
  1165. int i;
  1166. if (!smack_privileged(CAP_MAC_ADMIN))
  1167. return -EPERM;
  1168. if (count >= sizeof(temp) || count == 0)
  1169. return -EINVAL;
  1170. if (copy_from_user(temp, buf, count) != 0)
  1171. return -EFAULT;
  1172. temp[count] = '\0';
  1173. if (sscanf(temp, "%d", &i) != 1)
  1174. return -EINVAL;
  1175. smk_cipso_doi_value = i;
  1176. smk_cipso_doi();
  1177. return count;
  1178. }
  1179. static const struct file_operations smk_doi_ops = {
  1180. .read = smk_read_doi,
  1181. .write = smk_write_doi,
  1182. .llseek = default_llseek,
  1183. };
  1184. /**
  1185. * smk_read_direct - read() for /smack/direct
  1186. * @filp: file pointer, not actually used
  1187. * @buf: where to put the result
  1188. * @count: maximum to send along
  1189. * @ppos: where to start
  1190. *
  1191. * Returns number of bytes read or error code, as appropriate
  1192. */
  1193. static ssize_t smk_read_direct(struct file *filp, char __user *buf,
  1194. size_t count, loff_t *ppos)
  1195. {
  1196. char temp[80];
  1197. ssize_t rc;
  1198. if (*ppos != 0)
  1199. return 0;
  1200. sprintf(temp, "%d", smack_cipso_direct);
  1201. rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
  1202. return rc;
  1203. }
  1204. /**
  1205. * smk_write_direct - write() for /smack/direct
  1206. * @file: file pointer, not actually used
  1207. * @buf: where to get the data from
  1208. * @count: bytes sent
  1209. * @ppos: where to start
  1210. *
  1211. * Returns number of bytes written or error code, as appropriate
  1212. */
  1213. static ssize_t smk_write_direct(struct file *file, const char __user *buf,
  1214. size_t count, loff_t *ppos)
  1215. {
  1216. struct smack_known *skp;
  1217. char temp[80];
  1218. int i;
  1219. if (!smack_privileged(CAP_MAC_ADMIN))
  1220. return -EPERM;
  1221. if (count >= sizeof(temp) || count == 0)
  1222. return -EINVAL;
  1223. if (copy_from_user(temp, buf, count) != 0)
  1224. return -EFAULT;
  1225. temp[count] = '\0';
  1226. if (sscanf(temp, "%d", &i) != 1)
  1227. return -EINVAL;
  1228. /*
  1229. * Don't do anything if the value hasn't actually changed.
  1230. * If it is changing reset the level on entries that were
  1231. * set up to be direct when they were created.
  1232. */
  1233. if (smack_cipso_direct != i) {
  1234. mutex_lock(&smack_known_lock);
  1235. list_for_each_entry_rcu(skp, &smack_known_list, list)
  1236. if (skp->smk_netlabel.attr.mls.lvl ==
  1237. smack_cipso_direct)
  1238. skp->smk_netlabel.attr.mls.lvl = i;
  1239. smack_cipso_direct = i;
  1240. mutex_unlock(&smack_known_lock);
  1241. }
  1242. return count;
  1243. }
  1244. static const struct file_operations smk_direct_ops = {
  1245. .read = smk_read_direct,
  1246. .write = smk_write_direct,
  1247. .llseek = default_llseek,
  1248. };
  1249. /**
  1250. * smk_read_mapped - read() for /smack/mapped
  1251. * @filp: file pointer, not actually used
  1252. * @buf: where to put the result
  1253. * @count: maximum to send along
  1254. * @ppos: where to start
  1255. *
  1256. * Returns number of bytes read or error code, as appropriate
  1257. */
  1258. static ssize_t smk_read_mapped(struct file *filp, char __user *buf,
  1259. size_t count, loff_t *ppos)
  1260. {
  1261. char temp[80];
  1262. ssize_t rc;
  1263. if (*ppos != 0)
  1264. return 0;
  1265. sprintf(temp, "%d", smack_cipso_mapped);
  1266. rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
  1267. return rc;
  1268. }
  1269. /**
  1270. * smk_write_mapped - write() for /smack/mapped
  1271. * @file: file pointer, not actually used
  1272. * @buf: where to get the data from
  1273. * @count: bytes sent
  1274. * @ppos: where to start
  1275. *
  1276. * Returns number of bytes written or error code, as appropriate
  1277. */
  1278. static ssize_t smk_write_mapped(struct file *file, const char __user *buf,
  1279. size_t count, loff_t *ppos)
  1280. {
  1281. struct smack_known *skp;
  1282. char temp[80];
  1283. int i;
  1284. if (!smack_privileged(CAP_MAC_ADMIN))
  1285. return -EPERM;
  1286. if (count >= sizeof(temp) || count == 0)
  1287. return -EINVAL;
  1288. if (copy_from_user(temp, buf, count) != 0)
  1289. return -EFAULT;
  1290. temp[count] = '\0';
  1291. if (sscanf(temp, "%d", &i) != 1)
  1292. return -EINVAL;
  1293. /*
  1294. * Don't do anything if the value hasn't actually changed.
  1295. * If it is changing reset the level on entries that were
  1296. * set up to be mapped when they were created.
  1297. */
  1298. if (smack_cipso_mapped != i) {
  1299. mutex_lock(&smack_known_lock);
  1300. list_for_each_entry_rcu(skp, &smack_known_list, list)
  1301. if (skp->smk_netlabel.attr.mls.lvl ==
  1302. smack_cipso_mapped)
  1303. skp->smk_netlabel.attr.mls.lvl = i;
  1304. smack_cipso_mapped = i;
  1305. mutex_unlock(&smack_known_lock);
  1306. }
  1307. return count;
  1308. }
  1309. static const struct file_operations smk_mapped_ops = {
  1310. .read = smk_read_mapped,
  1311. .write = smk_write_mapped,
  1312. .llseek = default_llseek,
  1313. };
  1314. /**
  1315. * smk_read_ambient - read() for /smack/ambient
  1316. * @filp: file pointer, not actually used
  1317. * @buf: where to put the result
  1318. * @cn: maximum to send along
  1319. * @ppos: where to start
  1320. *
  1321. * Returns number of bytes read or error code, as appropriate
  1322. */
  1323. static ssize_t smk_read_ambient(struct file *filp, char __user *buf,
  1324. size_t cn, loff_t *ppos)
  1325. {
  1326. ssize_t rc;
  1327. int asize;
  1328. if (*ppos != 0)
  1329. return 0;
  1330. /*
  1331. * Being careful to avoid a problem in the case where
  1332. * smack_net_ambient gets changed in midstream.
  1333. */
  1334. mutex_lock(&smack_ambient_lock);
  1335. asize = strlen(smack_net_ambient) + 1;
  1336. if (cn >= asize)
  1337. rc = simple_read_from_buffer(buf, cn, ppos,
  1338. smack_net_ambient, asize);
  1339. else
  1340. rc = -EINVAL;
  1341. mutex_unlock(&smack_ambient_lock);
  1342. return rc;
  1343. }
  1344. /**
  1345. * smk_write_ambient - write() for /smack/ambient
  1346. * @file: file pointer, not actually used
  1347. * @buf: where to get the data from
  1348. * @count: bytes sent
  1349. * @ppos: where to start
  1350. *
  1351. * Returns number of bytes written or error code, as appropriate
  1352. */
  1353. static ssize_t smk_write_ambient(struct file *file, const char __user *buf,
  1354. size_t count, loff_t *ppos)
  1355. {
  1356. char *oldambient;
  1357. char *smack = NULL;
  1358. char *data;
  1359. int rc = count;
  1360. if (!smack_privileged(CAP_MAC_ADMIN))
  1361. return -EPERM;
  1362. data = kzalloc(count + 1, GFP_KERNEL);
  1363. if (data == NULL)
  1364. return -ENOMEM;
  1365. if (copy_from_user(data, buf, count) != 0) {
  1366. rc = -EFAULT;
  1367. goto out;
  1368. }
  1369. smack = smk_import(data, count);
  1370. if (smack == NULL) {
  1371. rc = -EINVAL;
  1372. goto out;
  1373. }
  1374. mutex_lock(&smack_ambient_lock);
  1375. oldambient = smack_net_ambient;
  1376. smack_net_ambient = smack;
  1377. smk_unlbl_ambient(oldambient);
  1378. mutex_unlock(&smack_ambient_lock);
  1379. out:
  1380. kfree(data);
  1381. return rc;
  1382. }
  1383. static const struct file_operations smk_ambient_ops = {
  1384. .read = smk_read_ambient,
  1385. .write = smk_write_ambient,
  1386. .llseek = default_llseek,
  1387. };
  1388. /**
  1389. * smk_read_onlycap - read() for /smack/onlycap
  1390. * @filp: file pointer, not actually used
  1391. * @buf: where to put the result
  1392. * @cn: maximum to send along
  1393. * @ppos: where to start
  1394. *
  1395. * Returns number of bytes read or error code, as appropriate
  1396. */
  1397. static ssize_t smk_read_onlycap(struct file *filp, char __user *buf,
  1398. size_t cn, loff_t *ppos)
  1399. {
  1400. char *smack = "";
  1401. ssize_t rc = -EINVAL;
  1402. int asize;
  1403. if (*ppos != 0)
  1404. return 0;
  1405. if (smack_onlycap != NULL)
  1406. smack = smack_onlycap;
  1407. asize = strlen(smack) + 1;
  1408. if (cn >= asize)
  1409. rc = simple_read_from_buffer(buf, cn, ppos, smack, asize);
  1410. return rc;
  1411. }
  1412. /**
  1413. * smk_write_onlycap - write() for /smack/onlycap
  1414. * @file: file pointer, not actually used
  1415. * @buf: where to get the data from
  1416. * @count: bytes sent
  1417. * @ppos: where to start
  1418. *
  1419. * Returns number of bytes written or error code, as appropriate
  1420. */
  1421. static ssize_t smk_write_onlycap(struct file *file, const char __user *buf,
  1422. size_t count, loff_t *ppos)
  1423. {
  1424. char *data;
  1425. char *sp = smk_of_task(current->cred->security);
  1426. int rc = count;
  1427. if (!smack_privileged(CAP_MAC_ADMIN))
  1428. return -EPERM;
  1429. /*
  1430. * This can be done using smk_access() but is done
  1431. * explicitly for clarity. The smk_access() implementation
  1432. * would use smk_access(smack_onlycap, MAY_WRITE)
  1433. */
  1434. if (smack_onlycap != NULL && smack_onlycap != sp)
  1435. return -EPERM;
  1436. data = kzalloc(count, GFP_KERNEL);
  1437. if (data == NULL)
  1438. return -ENOMEM;
  1439. /*
  1440. * Should the null string be passed in unset the onlycap value.
  1441. * This seems like something to be careful with as usually
  1442. * smk_import only expects to return NULL for errors. It
  1443. * is usually the case that a nullstring or "\n" would be
  1444. * bad to pass to smk_import but in fact this is useful here.
  1445. *
  1446. * smk_import will also reject a label beginning with '-',
  1447. * so "-usecapabilities" will also work.
  1448. */
  1449. if (copy_from_user(data, buf, count) != 0)
  1450. rc = -EFAULT;
  1451. else
  1452. smack_onlycap = smk_import(data, count);
  1453. kfree(data);
  1454. return rc;
  1455. }
  1456. static const struct file_operations smk_onlycap_ops = {
  1457. .read = smk_read_onlycap,
  1458. .write = smk_write_onlycap,
  1459. .llseek = default_llseek,
  1460. };
  1461. /**
  1462. * smk_read_logging - read() for /smack/logging
  1463. * @filp: file pointer, not actually used
  1464. * @buf: where to put the result
  1465. * @cn: maximum to send along
  1466. * @ppos: where to start
  1467. *
  1468. * Returns number of bytes read or error code, as appropriate
  1469. */
  1470. static ssize_t smk_read_logging(struct file *filp, char __user *buf,
  1471. size_t count, loff_t *ppos)
  1472. {
  1473. char temp[32];
  1474. ssize_t rc;
  1475. if (*ppos != 0)
  1476. return 0;
  1477. sprintf(temp, "%d\n", log_policy);
  1478. rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
  1479. return rc;
  1480. }
  1481. /**
  1482. * smk_write_logging - write() for /smack/logging
  1483. * @file: file pointer, not actually used
  1484. * @buf: where to get the data from
  1485. * @count: bytes sent
  1486. * @ppos: where to start
  1487. *
  1488. * Returns number of bytes written or error code, as appropriate
  1489. */
  1490. static ssize_t smk_write_logging(struct file *file, const char __user *buf,
  1491. size_t count, loff_t *ppos)
  1492. {
  1493. char temp[32];
  1494. int i;
  1495. if (!smack_privileged(CAP_MAC_ADMIN))
  1496. return -EPERM;
  1497. if (count >= sizeof(temp) || count == 0)
  1498. return -EINVAL;
  1499. if (copy_from_user(temp, buf, count) != 0)
  1500. return -EFAULT;
  1501. temp[count] = '\0';
  1502. if (sscanf(temp, "%d", &i) != 1)
  1503. return -EINVAL;
  1504. if (i < 0 || i > 3)
  1505. return -EINVAL;
  1506. log_policy = i;
  1507. return count;
  1508. }
  1509. static const struct file_operations smk_logging_ops = {
  1510. .read = smk_read_logging,
  1511. .write = smk_write_logging,
  1512. .llseek = default_llseek,
  1513. };
  1514. /*
  1515. * Seq_file read operations for /smack/load-self
  1516. */
  1517. static void *load_self_seq_start(struct seq_file *s, loff_t *pos)
  1518. {
  1519. struct task_smack *tsp = current_security();
  1520. return smk_seq_start(s, pos, &tsp->smk_rules);
  1521. }
  1522. static void *load_self_seq_next(struct seq_file *s, void *v, loff_t *pos)
  1523. {
  1524. struct task_smack *tsp = current_security();
  1525. return smk_seq_next(s, v, pos, &tsp->smk_rules);
  1526. }
  1527. static int load_self_seq_show(struct seq_file *s, void *v)
  1528. {
  1529. struct list_head *list = v;
  1530. struct smack_rule *srp =
  1531. list_entry(list, struct smack_rule, list);
  1532. smk_rule_show(s, srp, SMK_LABELLEN);
  1533. return 0;
  1534. }
  1535. static const struct seq_operations load_self_seq_ops = {
  1536. .start = load_self_seq_start,
  1537. .next = load_self_seq_next,
  1538. .show = load_self_seq_show,
  1539. .stop = smk_seq_stop,
  1540. };
  1541. /**
  1542. * smk_open_load_self - open() for /smack/load-self2
  1543. * @inode: inode structure representing file
  1544. * @file: "load" file pointer
  1545. *
  1546. * For reading, use load_seq_* seq_file reading operations.
  1547. */
  1548. static int smk_open_load_self(struct inode *inode, struct file *file)
  1549. {
  1550. return seq_open(file, &load_self_seq_ops);
  1551. }
  1552. /**
  1553. * smk_write_load_self - write() for /smack/load-self
  1554. * @file: file pointer, not actually used
  1555. * @buf: where to get the data from
  1556. * @count: bytes sent
  1557. * @ppos: where to start - must be 0
  1558. *
  1559. */
  1560. static ssize_t smk_write_load_self(struct file *file, const char __user *buf,
  1561. size_t count, loff_t *ppos)
  1562. {
  1563. struct task_smack *tsp = current_security();
  1564. return smk_write_rules_list(file, buf, count, ppos, &tsp->smk_rules,
  1565. &tsp->smk_rules_lock, SMK_FIXED24_FMT);
  1566. }
  1567. static const struct file_operations smk_load_self_ops = {
  1568. .open = smk_open_load_self,
  1569. .read = seq_read,
  1570. .llseek = seq_lseek,
  1571. .write = smk_write_load_self,
  1572. .release = seq_release,
  1573. };
  1574. /**
  1575. * smk_user_access - handle access check transaction
  1576. * @file: file pointer
  1577. * @buf: data from user space
  1578. * @count: bytes sent
  1579. * @ppos: where to start - must be 0
  1580. */
  1581. static ssize_t smk_user_access(struct file *file, const char __user *buf,
  1582. size_t count, loff_t *ppos, int format)
  1583. {
  1584. struct smack_parsed_rule rule;
  1585. char *data;
  1586. char *cod;
  1587. int res;
  1588. data = simple_transaction_get(file, buf, count);
  1589. if (IS_ERR(data))
  1590. return PTR_ERR(data);
  1591. if (format == SMK_FIXED24_FMT) {
  1592. if (count < SMK_LOADLEN)
  1593. return -EINVAL;
  1594. res = smk_parse_rule(data, &rule, 0);
  1595. } else {
  1596. /*
  1597. * Copy the data to make sure the string is terminated.
  1598. */
  1599. cod = kzalloc(count + 1, GFP_KERNEL);
  1600. if (cod == NULL)
  1601. return -ENOMEM;
  1602. memcpy(cod, data, count);
  1603. cod[count] = '\0';
  1604. res = smk_parse_long_rule(cod, &rule, 0, 0);
  1605. kfree(cod);
  1606. }
  1607. if (res)
  1608. return -EINVAL;
  1609. res = smk_access(rule.smk_subject, rule.smk_object, rule.smk_access1,
  1610. NULL);
  1611. data[0] = res == 0 ? '1' : '0';
  1612. data[1] = '\0';
  1613. simple_transaction_set(file, 2);
  1614. if (format == SMK_FIXED24_FMT)
  1615. return SMK_LOADLEN;
  1616. return count;
  1617. }
  1618. /**
  1619. * smk_write_access - handle access check transaction
  1620. * @file: file pointer
  1621. * @buf: data from user space
  1622. * @count: bytes sent
  1623. * @ppos: where to start - must be 0
  1624. */
  1625. static ssize_t smk_write_access(struct file *file, const char __user *buf,
  1626. size_t count, loff_t *ppos)
  1627. {
  1628. return smk_user_access(file, buf, count, ppos, SMK_FIXED24_FMT);
  1629. }
  1630. static const struct file_operations smk_access_ops = {
  1631. .write = smk_write_access,
  1632. .read = simple_transaction_read,
  1633. .release = simple_transaction_release,
  1634. .llseek = generic_file_llseek,
  1635. };
  1636. /*
  1637. * Seq_file read operations for /smack/load2
  1638. */
  1639. static int load2_seq_show(struct seq_file *s, void *v)
  1640. {
  1641. struct list_head *list = v;
  1642. struct smack_master_list *smlp =
  1643. list_entry(list, struct smack_master_list, list);
  1644. smk_rule_show(s, smlp->smk_rule, SMK_LONGLABEL);
  1645. return 0;
  1646. }
  1647. static const struct seq_operations load2_seq_ops = {
  1648. .start = load2_seq_start,
  1649. .next = load2_seq_next,
  1650. .show = load2_seq_show,
  1651. .stop = smk_seq_stop,
  1652. };
  1653. /**
  1654. * smk_open_load2 - open() for /smack/load2
  1655. * @inode: inode structure representing file
  1656. * @file: "load2" file pointer
  1657. *
  1658. * For reading, use load2_seq_* seq_file reading operations.
  1659. */
  1660. static int smk_open_load2(struct inode *inode, struct file *file)
  1661. {
  1662. return seq_open(file, &load2_seq_ops);
  1663. }
  1664. /**
  1665. * smk_write_load2 - write() for /smack/load2
  1666. * @file: file pointer, not actually used
  1667. * @buf: where to get the data from
  1668. * @count: bytes sent
  1669. * @ppos: where to start - must be 0
  1670. *
  1671. */
  1672. static ssize_t smk_write_load2(struct file *file, const char __user *buf,
  1673. size_t count, loff_t *ppos)
  1674. {
  1675. /*
  1676. * Must have privilege.
  1677. */
  1678. if (!smack_privileged(CAP_MAC_ADMIN))
  1679. return -EPERM;
  1680. return smk_write_rules_list(file, buf, count, ppos, NULL, NULL,
  1681. SMK_LONG_FMT);
  1682. }
  1683. static const struct file_operations smk_load2_ops = {
  1684. .open = smk_open_load2,
  1685. .read = seq_read,
  1686. .llseek = seq_lseek,
  1687. .write = smk_write_load2,
  1688. .release = seq_release,
  1689. };
  1690. /*
  1691. * Seq_file read operations for /smack/load-self2
  1692. */
  1693. static void *load_self2_seq_start(struct seq_file *s, loff_t *pos)
  1694. {
  1695. struct task_smack *tsp = current_security();
  1696. return smk_seq_start(s, pos, &tsp->smk_rules);
  1697. }
  1698. static void *load_self2_seq_next(struct seq_file *s, void *v, loff_t *pos)
  1699. {
  1700. struct task_smack *tsp = current_security();
  1701. return smk_seq_next(s, v, pos, &tsp->smk_rules);
  1702. }
  1703. static int load_self2_seq_show(struct seq_file *s, void *v)
  1704. {
  1705. struct list_head *list = v;
  1706. struct smack_rule *srp =
  1707. list_entry(list, struct smack_rule, list);
  1708. smk_rule_show(s, srp, SMK_LONGLABEL);
  1709. return 0;
  1710. }
  1711. static const struct seq_operations load_self2_seq_ops = {
  1712. .start = load_self2_seq_start,
  1713. .next = load_self2_seq_next,
  1714. .show = load_self2_seq_show,
  1715. .stop = smk_seq_stop,
  1716. };
  1717. /**
  1718. * smk_open_load_self2 - open() for /smack/load-self2
  1719. * @inode: inode structure representing file
  1720. * @file: "load" file pointer
  1721. *
  1722. * For reading, use load_seq_* seq_file reading operations.
  1723. */
  1724. static int smk_open_load_self2(struct inode *inode, struct file *file)
  1725. {
  1726. return seq_open(file, &load_self2_seq_ops);
  1727. }
  1728. /**
  1729. * smk_write_load_self2 - write() for /smack/load-self2
  1730. * @file: file pointer, not actually used
  1731. * @buf: where to get the data from
  1732. * @count: bytes sent
  1733. * @ppos: where to start - must be 0
  1734. *
  1735. */
  1736. static ssize_t smk_write_load_self2(struct file *file, const char __user *buf,
  1737. size_t count, loff_t *ppos)
  1738. {
  1739. struct task_smack *tsp = current_security();
  1740. return smk_write_rules_list(file, buf, count, ppos, &tsp->smk_rules,
  1741. &tsp->smk_rules_lock, SMK_LONG_FMT);
  1742. }
  1743. static const struct file_operations smk_load_self2_ops = {
  1744. .open = smk_open_load_self2,
  1745. .read = seq_read,
  1746. .llseek = seq_lseek,
  1747. .write = smk_write_load_self2,
  1748. .release = seq_release,
  1749. };
  1750. /**
  1751. * smk_write_access2 - handle access check transaction
  1752. * @file: file pointer
  1753. * @buf: data from user space
  1754. * @count: bytes sent
  1755. * @ppos: where to start - must be 0
  1756. */
  1757. static ssize_t smk_write_access2(struct file *file, const char __user *buf,
  1758. size_t count, loff_t *ppos)
  1759. {
  1760. return smk_user_access(file, buf, count, ppos, SMK_LONG_FMT);
  1761. }
  1762. static const struct file_operations smk_access2_ops = {
  1763. .write = smk_write_access2,
  1764. .read = simple_transaction_read,
  1765. .release = simple_transaction_release,
  1766. .llseek = generic_file_llseek,
  1767. };
  1768. /**
  1769. * smk_write_revoke_subj - write() for /smack/revoke-subject
  1770. * @file: file pointer
  1771. * @buf: data from user space
  1772. * @count: bytes sent
  1773. * @ppos: where to start - must be 0
  1774. */
  1775. static ssize_t smk_write_revoke_subj(struct file *file, const char __user *buf,
  1776. size_t count, loff_t *ppos)
  1777. {
  1778. char *data = NULL;
  1779. const char *cp = NULL;
  1780. struct smack_known *skp;
  1781. struct smack_rule *sp;
  1782. struct list_head *rule_list;
  1783. struct mutex *rule_lock;
  1784. int rc = count;
  1785. if (*ppos != 0)
  1786. return -EINVAL;
  1787. if (!smack_privileged(CAP_MAC_ADMIN))
  1788. return -EPERM;
  1789. if (count == 0 || count > SMK_LONGLABEL)
  1790. return -EINVAL;
  1791. data = kzalloc(count, GFP_KERNEL);
  1792. if (data == NULL)
  1793. return -ENOMEM;
  1794. if (copy_from_user(data, buf, count) != 0) {
  1795. rc = -EFAULT;
  1796. goto free_out;
  1797. }
  1798. cp = smk_parse_smack(data, count);
  1799. if (cp == NULL) {
  1800. rc = -EINVAL;
  1801. goto free_out;
  1802. }
  1803. skp = smk_find_entry(cp);
  1804. if (skp == NULL)
  1805. goto free_out;
  1806. rule_list = &skp->smk_rules;
  1807. rule_lock = &skp->smk_rules_lock;
  1808. mutex_lock(rule_lock);
  1809. list_for_each_entry_rcu(sp, rule_list, list)
  1810. sp->smk_access = 0;
  1811. mutex_unlock(rule_lock);
  1812. free_out:
  1813. kfree(data);
  1814. kfree(cp);
  1815. return rc;
  1816. }
  1817. static const struct file_operations smk_revoke_subj_ops = {
  1818. .write = smk_write_revoke_subj,
  1819. .read = simple_transaction_read,
  1820. .release = simple_transaction_release,
  1821. .llseek = generic_file_llseek,
  1822. };
  1823. static struct kset *smackfs_kset;
  1824. /**
  1825. * smk_init_sysfs - initialize /sys/fs/smackfs
  1826. *
  1827. */
  1828. static int smk_init_sysfs(void)
  1829. {
  1830. smackfs_kset = kset_create_and_add("smackfs", NULL, fs_kobj);
  1831. if (!smackfs_kset)
  1832. return -ENOMEM;
  1833. return 0;
  1834. }
  1835. /**
  1836. * smk_write_change_rule - write() for /smack/change-rule
  1837. * @file: file pointer
  1838. * @buf: data from user space
  1839. * @count: bytes sent
  1840. * @ppos: where to start - must be 0
  1841. */
  1842. static ssize_t smk_write_change_rule(struct file *file, const char __user *buf,
  1843. size_t count, loff_t *ppos)
  1844. {
  1845. /*
  1846. * Must have privilege.
  1847. */
  1848. if (!capable(CAP_MAC_ADMIN))
  1849. return -EPERM;
  1850. return smk_write_rules_list(file, buf, count, ppos, NULL, NULL,
  1851. SMK_CHANGE_FMT);
  1852. }
  1853. static const struct file_operations smk_change_rule_ops = {
  1854. .write = smk_write_change_rule,
  1855. .read = simple_transaction_read,
  1856. .release = simple_transaction_release,
  1857. .llseek = generic_file_llseek,
  1858. };
  1859. /**
  1860. * smk_fill_super - fill the /smackfs superblock
  1861. * @sb: the empty superblock
  1862. * @data: unused
  1863. * @silent: unused
  1864. *
  1865. * Fill in the well known entries for /smack
  1866. *
  1867. * Returns 0 on success, an error code on failure
  1868. */
  1869. static int smk_fill_super(struct super_block *sb, void *data, int silent)
  1870. {
  1871. int rc;
  1872. struct inode *root_inode;
  1873. static struct tree_descr smack_files[] = {
  1874. [SMK_LOAD] = {
  1875. "load", &smk_load_ops, S_IRUGO|S_IWUSR},
  1876. [SMK_CIPSO] = {
  1877. "cipso", &smk_cipso_ops, S_IRUGO|S_IWUSR},
  1878. [SMK_DOI] = {
  1879. "doi", &smk_doi_ops, S_IRUGO|S_IWUSR},
  1880. [SMK_DIRECT] = {
  1881. "direct", &smk_direct_ops, S_IRUGO|S_IWUSR},
  1882. [SMK_AMBIENT] = {
  1883. "ambient", &smk_ambient_ops, S_IRUGO|S_IWUSR},
  1884. [SMK_NETLBLADDR] = {
  1885. "netlabel", &smk_netlbladdr_ops, S_IRUGO|S_IWUSR},
  1886. [SMK_ONLYCAP] = {
  1887. "onlycap", &smk_onlycap_ops, S_IRUGO|S_IWUSR},
  1888. [SMK_LOGGING] = {
  1889. "logging", &smk_logging_ops, S_IRUGO|S_IWUSR},
  1890. [SMK_LOAD_SELF] = {
  1891. "load-self", &smk_load_self_ops, S_IRUGO|S_IWUGO},
  1892. [SMK_ACCESSES] = {
  1893. "access", &smk_access_ops, S_IRUGO|S_IWUGO},
  1894. [SMK_MAPPED] = {
  1895. "mapped", &smk_mapped_ops, S_IRUGO|S_IWUSR},
  1896. [SMK_LOAD2] = {
  1897. "load2", &smk_load2_ops, S_IRUGO|S_IWUSR},
  1898. [SMK_LOAD_SELF2] = {
  1899. "load-self2", &smk_load_self2_ops, S_IRUGO|S_IWUGO},
  1900. [SMK_ACCESS2] = {
  1901. "access2", &smk_access2_ops, S_IRUGO|S_IWUGO},
  1902. [SMK_CIPSO2] = {
  1903. "cipso2", &smk_cipso2_ops, S_IRUGO|S_IWUSR},
  1904. [SMK_REVOKE_SUBJ] = {
  1905. "revoke-subject", &smk_revoke_subj_ops,
  1906. S_IRUGO|S_IWUSR},
  1907. [SMK_CHANGE_RULE] = {
  1908. "change-rule", &smk_change_rule_ops, S_IRUGO|S_IWUSR},
  1909. /* last one */
  1910. {""}
  1911. };
  1912. rc = simple_fill_super(sb, SMACK_MAGIC, smack_files);
  1913. if (rc != 0) {
  1914. printk(KERN_ERR "%s failed %d while creating inodes\n",
  1915. __func__, rc);
  1916. return rc;
  1917. }
  1918. root_inode = sb->s_root->d_inode;
  1919. return 0;
  1920. }
  1921. /**
  1922. * smk_mount - get the smackfs superblock
  1923. * @fs_type: passed along without comment
  1924. * @flags: passed along without comment
  1925. * @dev_name: passed along without comment
  1926. * @data: passed along without comment
  1927. *
  1928. * Just passes everything along.
  1929. *
  1930. * Returns what the lower level code does.
  1931. */
  1932. static struct dentry *smk_mount(struct file_system_type *fs_type,
  1933. int flags, const char *dev_name, void *data)
  1934. {
  1935. return mount_single(fs_type, flags, data, smk_fill_super);
  1936. }
  1937. static struct file_system_type smk_fs_type = {
  1938. .name = "smackfs",
  1939. .mount = smk_mount,
  1940. .kill_sb = kill_litter_super,
  1941. };
  1942. static struct vfsmount *smackfs_mount;
  1943. static int __init smk_preset_netlabel(struct smack_known *skp)
  1944. {
  1945. skp->smk_netlabel.domain = skp->smk_known;
  1946. skp->smk_netlabel.flags =
  1947. NETLBL_SECATTR_DOMAIN | NETLBL_SECATTR_MLS_LVL;
  1948. return smk_netlbl_mls(smack_cipso_direct, skp->smk_known,
  1949. &skp->smk_netlabel, strlen(skp->smk_known));
  1950. }
  1951. /**
  1952. * init_smk_fs - get the smackfs superblock
  1953. *
  1954. * register the smackfs
  1955. *
  1956. * Do not register smackfs if Smack wasn't enabled
  1957. * on boot. We can not put this method normally under the
  1958. * smack_init() code path since the security subsystem get
  1959. * initialized before the vfs caches.
  1960. *
  1961. * Returns true if we were not chosen on boot or if
  1962. * we were chosen and filesystem registration succeeded.
  1963. */
  1964. static int __init init_smk_fs(void)
  1965. {
  1966. int err;
  1967. int rc;
  1968. if (!security_module_enable(&smack_ops))
  1969. return 0;
  1970. err = smk_init_sysfs();
  1971. if (err)
  1972. printk(KERN_ERR "smackfs: sysfs mountpoint problem.\n");
  1973. err = register_filesystem(&smk_fs_type);
  1974. if (!err) {
  1975. smackfs_mount = kern_mount(&smk_fs_type);
  1976. if (IS_ERR(smackfs_mount)) {
  1977. printk(KERN_ERR "smackfs: could not mount!\n");
  1978. err = PTR_ERR(smackfs_mount);
  1979. smackfs_mount = NULL;
  1980. }
  1981. }
  1982. smk_cipso_doi();
  1983. smk_unlbl_ambient(NULL);
  1984. rc = smk_preset_netlabel(&smack_known_floor);
  1985. if (err == 0 && rc < 0)
  1986. err = rc;
  1987. rc = smk_preset_netlabel(&smack_known_hat);
  1988. if (err == 0 && rc < 0)
  1989. err = rc;
  1990. rc = smk_preset_netlabel(&smack_known_huh);
  1991. if (err == 0 && rc < 0)
  1992. err = rc;
  1993. rc = smk_preset_netlabel(&smack_known_invalid);
  1994. if (err == 0 && rc < 0)
  1995. err = rc;
  1996. rc = smk_preset_netlabel(&smack_known_star);
  1997. if (err == 0 && rc < 0)
  1998. err = rc;
  1999. rc = smk_preset_netlabel(&smack_known_web);
  2000. if (err == 0 && rc < 0)
  2001. err = rc;
  2002. return err;
  2003. }
  2004. __initcall(init_smk_fs);