smackfs.c 56 KB

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