smackfs.c 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937
  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. #define BEBITS (sizeof(__be32) * 8)
  31. /*
  32. * smackfs pseudo filesystem.
  33. */
  34. enum smk_inos {
  35. SMK_ROOT_INO = 2,
  36. SMK_LOAD = 3, /* load policy */
  37. SMK_CIPSO = 4, /* load label -> CIPSO mapping */
  38. SMK_DOI = 5, /* CIPSO DOI */
  39. SMK_DIRECT = 6, /* CIPSO level indicating direct label */
  40. SMK_AMBIENT = 7, /* internet ambient label */
  41. SMK_NET4ADDR = 8, /* single label hosts */
  42. SMK_ONLYCAP = 9, /* the only "capable" label */
  43. SMK_LOGGING = 10, /* logging */
  44. SMK_LOAD_SELF = 11, /* task specific rules */
  45. SMK_ACCESSES = 12, /* access policy */
  46. SMK_MAPPED = 13, /* CIPSO level indicating mapped label */
  47. SMK_LOAD2 = 14, /* load policy with long labels */
  48. SMK_LOAD_SELF2 = 15, /* load task specific rules with long labels */
  49. SMK_ACCESS2 = 16, /* make an access check with long labels */
  50. SMK_CIPSO2 = 17, /* load long label -> CIPSO mapping */
  51. SMK_REVOKE_SUBJ = 18, /* set rules with subject label to '-' */
  52. SMK_CHANGE_RULE = 19, /* change or add rules (long labels) */
  53. SMK_SYSLOG = 20, /* change syslog label) */
  54. SMK_PTRACE = 21, /* set ptrace rule */
  55. #ifdef CONFIG_SECURITY_SMACK_BRINGUP
  56. SMK_UNCONFINED = 22, /* define an unconfined label */
  57. #endif
  58. #if IS_ENABLED(CONFIG_IPV6)
  59. SMK_NET6ADDR = 23, /* single label IPv6 hosts */
  60. #endif /* CONFIG_IPV6 */
  61. };
  62. /*
  63. * List locks
  64. */
  65. static DEFINE_MUTEX(smack_cipso_lock);
  66. static DEFINE_MUTEX(smack_ambient_lock);
  67. static DEFINE_MUTEX(smk_net4addr_lock);
  68. #if IS_ENABLED(CONFIG_IPV6)
  69. static DEFINE_MUTEX(smk_net6addr_lock);
  70. #endif /* CONFIG_IPV6 */
  71. /*
  72. * This is the "ambient" label for network traffic.
  73. * If it isn't somehow marked, use this.
  74. * It can be reset via smackfs/ambient
  75. */
  76. struct smack_known *smack_net_ambient;
  77. /*
  78. * This is the level in a CIPSO header that indicates a
  79. * smack label is contained directly in the category set.
  80. * It can be reset via smackfs/direct
  81. */
  82. int smack_cipso_direct = SMACK_CIPSO_DIRECT_DEFAULT;
  83. /*
  84. * This is the level in a CIPSO header that indicates a
  85. * secid is contained directly in the category set.
  86. * It can be reset via smackfs/mapped
  87. */
  88. int smack_cipso_mapped = SMACK_CIPSO_MAPPED_DEFAULT;
  89. #ifdef CONFIG_SECURITY_SMACK_BRINGUP
  90. /*
  91. * Allow one label to be unconfined. This is for
  92. * debugging and application bring-up purposes only.
  93. * It is bad and wrong, but everyone seems to expect
  94. * to have it.
  95. */
  96. struct smack_known *smack_unconfined;
  97. #endif
  98. /*
  99. * If this value is set restrict syslog use to the label specified.
  100. * It can be reset via smackfs/syslog
  101. */
  102. struct smack_known *smack_syslog_label;
  103. /*
  104. * Ptrace current rule
  105. * SMACK_PTRACE_DEFAULT regular smack ptrace rules (/proc based)
  106. * SMACK_PTRACE_EXACT labels must match, but can be overriden with
  107. * CAP_SYS_PTRACE
  108. * SMACK_PTRACE_DRACONIAN lables must match, CAP_SYS_PTRACE has no effect
  109. */
  110. int smack_ptrace_rule = SMACK_PTRACE_DEFAULT;
  111. /*
  112. * Certain IP addresses may be designated as single label hosts.
  113. * Packets are sent there unlabeled, but only from tasks that
  114. * can write to the specified label.
  115. */
  116. LIST_HEAD(smk_net4addr_list);
  117. #if IS_ENABLED(CONFIG_IPV6)
  118. LIST_HEAD(smk_net6addr_list);
  119. #endif /* CONFIG_IPV6 */
  120. /*
  121. * Rule lists are maintained for each label.
  122. * This master list is just for reading /smack/load and /smack/load2.
  123. */
  124. struct smack_master_list {
  125. struct list_head list;
  126. struct smack_rule *smk_rule;
  127. };
  128. static LIST_HEAD(smack_rule_list);
  129. struct smack_parsed_rule {
  130. struct smack_known *smk_subject;
  131. struct smack_known *smk_object;
  132. int smk_access1;
  133. int smk_access2;
  134. };
  135. static int smk_cipso_doi_value = SMACK_CIPSO_DOI_DEFAULT;
  136. /*
  137. * Values for parsing cipso rules
  138. * SMK_DIGITLEN: Length of a digit field in a rule.
  139. * SMK_CIPSOMIN: Minimum possible cipso rule length.
  140. * SMK_CIPSOMAX: Maximum possible cipso rule length.
  141. */
  142. #define SMK_DIGITLEN 4
  143. #define SMK_CIPSOMIN (SMK_LABELLEN + 2 * SMK_DIGITLEN)
  144. #define SMK_CIPSOMAX (SMK_CIPSOMIN + SMACK_CIPSO_MAXCATNUM * SMK_DIGITLEN)
  145. /*
  146. * Values for parsing MAC rules
  147. * SMK_ACCESS: Maximum possible combination of access permissions
  148. * SMK_ACCESSLEN: Maximum length for a rule access field
  149. * SMK_LOADLEN: Smack rule length
  150. */
  151. #define SMK_OACCESS "rwxa"
  152. #define SMK_ACCESS "rwxatl"
  153. #define SMK_OACCESSLEN (sizeof(SMK_OACCESS) - 1)
  154. #define SMK_ACCESSLEN (sizeof(SMK_ACCESS) - 1)
  155. #define SMK_OLOADLEN (SMK_LABELLEN + SMK_LABELLEN + SMK_OACCESSLEN)
  156. #define SMK_LOADLEN (SMK_LABELLEN + SMK_LABELLEN + SMK_ACCESSLEN)
  157. /*
  158. * Stricly for CIPSO level manipulation.
  159. * Set the category bit number in a smack label sized buffer.
  160. */
  161. static inline void smack_catset_bit(unsigned int cat, char *catsetp)
  162. {
  163. if (cat == 0 || cat > (SMK_CIPSOLEN * 8))
  164. return;
  165. catsetp[(cat - 1) / 8] |= 0x80 >> ((cat - 1) % 8);
  166. }
  167. /**
  168. * smk_netlabel_audit_set - fill a netlbl_audit struct
  169. * @nap: structure to fill
  170. */
  171. static void smk_netlabel_audit_set(struct netlbl_audit *nap)
  172. {
  173. struct smack_known *skp = smk_of_current();
  174. nap->loginuid = audit_get_loginuid(current);
  175. nap->sessionid = audit_get_sessionid(current);
  176. nap->secid = skp->smk_secid;
  177. }
  178. /*
  179. * Value for parsing single label host rules
  180. * "1.2.3.4 X"
  181. */
  182. #define SMK_NETLBLADDRMIN 9
  183. /**
  184. * smk_set_access - add a rule to the rule list or replace an old rule
  185. * @srp: the rule to add or replace
  186. * @rule_list: the list of rules
  187. * @rule_lock: the rule list lock
  188. * @global: if non-zero, indicates a global rule
  189. *
  190. * Looks through the current subject/object/access list for
  191. * the subject/object pair and replaces the access that was
  192. * there. If the pair isn't found add it with the specified
  193. * access.
  194. *
  195. * Returns 0 if nothing goes wrong or -ENOMEM if it fails
  196. * during the allocation of the new pair to add.
  197. */
  198. static int smk_set_access(struct smack_parsed_rule *srp,
  199. struct list_head *rule_list,
  200. struct mutex *rule_lock, int global)
  201. {
  202. struct smack_rule *sp;
  203. struct smack_master_list *smlp;
  204. int found = 0;
  205. int rc = 0;
  206. mutex_lock(rule_lock);
  207. /*
  208. * Because the object label is less likely to match
  209. * than the subject label check it first
  210. */
  211. list_for_each_entry_rcu(sp, rule_list, list) {
  212. if (sp->smk_object == srp->smk_object &&
  213. sp->smk_subject == srp->smk_subject) {
  214. found = 1;
  215. sp->smk_access |= srp->smk_access1;
  216. sp->smk_access &= ~srp->smk_access2;
  217. break;
  218. }
  219. }
  220. if (found == 0) {
  221. sp = kzalloc(sizeof(*sp), GFP_KERNEL);
  222. if (sp == NULL) {
  223. rc = -ENOMEM;
  224. goto out;
  225. }
  226. sp->smk_subject = srp->smk_subject;
  227. sp->smk_object = srp->smk_object;
  228. sp->smk_access = srp->smk_access1 & ~srp->smk_access2;
  229. list_add_rcu(&sp->list, rule_list);
  230. /*
  231. * If this is a global as opposed to self and a new rule
  232. * it needs to get added for reporting.
  233. */
  234. if (global) {
  235. smlp = kzalloc(sizeof(*smlp), GFP_KERNEL);
  236. if (smlp != NULL) {
  237. smlp->smk_rule = sp;
  238. list_add_rcu(&smlp->list, &smack_rule_list);
  239. } else
  240. rc = -ENOMEM;
  241. }
  242. }
  243. out:
  244. mutex_unlock(rule_lock);
  245. return rc;
  246. }
  247. /**
  248. * smk_perm_from_str - parse smack accesses from a text string
  249. * @string: a text string that contains a Smack accesses code
  250. *
  251. * Returns an integer with respective bits set for specified accesses.
  252. */
  253. static int smk_perm_from_str(const char *string)
  254. {
  255. int perm = 0;
  256. const char *cp;
  257. for (cp = string; ; cp++)
  258. switch (*cp) {
  259. case '-':
  260. break;
  261. case 'r':
  262. case 'R':
  263. perm |= MAY_READ;
  264. break;
  265. case 'w':
  266. case 'W':
  267. perm |= MAY_WRITE;
  268. break;
  269. case 'x':
  270. case 'X':
  271. perm |= MAY_EXEC;
  272. break;
  273. case 'a':
  274. case 'A':
  275. perm |= MAY_APPEND;
  276. break;
  277. case 't':
  278. case 'T':
  279. perm |= MAY_TRANSMUTE;
  280. break;
  281. case 'l':
  282. case 'L':
  283. perm |= MAY_LOCK;
  284. break;
  285. case 'b':
  286. case 'B':
  287. perm |= MAY_BRINGUP;
  288. break;
  289. default:
  290. return perm;
  291. }
  292. }
  293. /**
  294. * smk_fill_rule - Fill Smack rule from strings
  295. * @subject: subject label string
  296. * @object: object label string
  297. * @access1: access string
  298. * @access2: string with permissions to be removed
  299. * @rule: Smack rule
  300. * @import: if non-zero, import labels
  301. * @len: label length limit
  302. *
  303. * Returns 0 on success, appropriate error code on failure.
  304. */
  305. static int smk_fill_rule(const char *subject, const char *object,
  306. const char *access1, const char *access2,
  307. struct smack_parsed_rule *rule, int import,
  308. int len)
  309. {
  310. const char *cp;
  311. struct smack_known *skp;
  312. if (import) {
  313. rule->smk_subject = smk_import_entry(subject, len);
  314. if (IS_ERR(rule->smk_subject))
  315. return PTR_ERR(rule->smk_subject);
  316. rule->smk_object = smk_import_entry(object, len);
  317. if (IS_ERR(rule->smk_object))
  318. return PTR_ERR(rule->smk_object);
  319. } else {
  320. cp = smk_parse_smack(subject, len);
  321. if (IS_ERR(cp))
  322. return PTR_ERR(cp);
  323. skp = smk_find_entry(cp);
  324. kfree(cp);
  325. if (skp == NULL)
  326. return -ENOENT;
  327. rule->smk_subject = skp;
  328. cp = smk_parse_smack(object, len);
  329. if (IS_ERR(cp))
  330. return PTR_ERR(cp);
  331. skp = smk_find_entry(cp);
  332. kfree(cp);
  333. if (skp == NULL)
  334. return -ENOENT;
  335. rule->smk_object = skp;
  336. }
  337. rule->smk_access1 = smk_perm_from_str(access1);
  338. if (access2)
  339. rule->smk_access2 = smk_perm_from_str(access2);
  340. else
  341. rule->smk_access2 = ~rule->smk_access1;
  342. return 0;
  343. }
  344. /**
  345. * smk_parse_rule - parse Smack rule from load string
  346. * @data: string to be parsed whose size is SMK_LOADLEN
  347. * @rule: Smack rule
  348. * @import: if non-zero, import labels
  349. *
  350. * Returns 0 on success, -1 on errors.
  351. */
  352. static int smk_parse_rule(const char *data, struct smack_parsed_rule *rule,
  353. int import)
  354. {
  355. int rc;
  356. rc = smk_fill_rule(data, data + SMK_LABELLEN,
  357. data + SMK_LABELLEN + SMK_LABELLEN, NULL, rule,
  358. import, SMK_LABELLEN);
  359. return rc;
  360. }
  361. /**
  362. * smk_parse_long_rule - parse Smack rule from rule string
  363. * @data: string to be parsed, null terminated
  364. * @rule: Will be filled with Smack parsed rule
  365. * @import: if non-zero, import labels
  366. * @tokens: numer of substrings expected in data
  367. *
  368. * Returns number of processed bytes on success, -ERRNO on failure.
  369. */
  370. static ssize_t smk_parse_long_rule(char *data, struct smack_parsed_rule *rule,
  371. int import, int tokens)
  372. {
  373. ssize_t cnt = 0;
  374. char *tok[4];
  375. int rc;
  376. int i;
  377. /*
  378. * Parsing the rule in-place, filling all white-spaces with '\0'
  379. */
  380. for (i = 0; i < tokens; ++i) {
  381. while (isspace(data[cnt]))
  382. data[cnt++] = '\0';
  383. if (data[cnt] == '\0')
  384. /* Unexpected end of data */
  385. return -EINVAL;
  386. tok[i] = data + cnt;
  387. while (data[cnt] && !isspace(data[cnt]))
  388. ++cnt;
  389. }
  390. while (isspace(data[cnt]))
  391. data[cnt++] = '\0';
  392. while (i < 4)
  393. tok[i++] = NULL;
  394. rc = smk_fill_rule(tok[0], tok[1], tok[2], tok[3], rule, import, 0);
  395. return rc == 0 ? cnt : rc;
  396. }
  397. #define SMK_FIXED24_FMT 0 /* Fixed 24byte label format */
  398. #define SMK_LONG_FMT 1 /* Variable long label format */
  399. #define SMK_CHANGE_FMT 2 /* Rule modification format */
  400. /**
  401. * smk_write_rules_list - write() for any /smack rule file
  402. * @file: file pointer, not actually used
  403. * @buf: where to get the data from
  404. * @count: bytes sent
  405. * @ppos: where to start - must be 0
  406. * @rule_list: the list of rules to write to
  407. * @rule_lock: lock for the rule list
  408. * @format: /smack/load or /smack/load2 or /smack/change-rule format.
  409. *
  410. * Get one smack access rule from above.
  411. * The format for SMK_LONG_FMT is:
  412. * "subject<whitespace>object<whitespace>access[<whitespace>...]"
  413. * The format for SMK_FIXED24_FMT is exactly:
  414. * "subject object rwxat"
  415. * The format for SMK_CHANGE_FMT is:
  416. * "subject<whitespace>object<whitespace>
  417. * acc_enable<whitespace>acc_disable[<whitespace>...]"
  418. */
  419. static ssize_t smk_write_rules_list(struct file *file, const char __user *buf,
  420. size_t count, loff_t *ppos,
  421. struct list_head *rule_list,
  422. struct mutex *rule_lock, int format)
  423. {
  424. struct smack_parsed_rule rule;
  425. char *data;
  426. int rc;
  427. int trunc = 0;
  428. int tokens;
  429. ssize_t cnt = 0;
  430. /*
  431. * No partial writes.
  432. * Enough data must be present.
  433. */
  434. if (*ppos != 0)
  435. return -EINVAL;
  436. if (format == SMK_FIXED24_FMT) {
  437. /*
  438. * Minor hack for backward compatibility
  439. */
  440. if (count < SMK_OLOADLEN || count > SMK_LOADLEN)
  441. return -EINVAL;
  442. } else {
  443. if (count >= PAGE_SIZE) {
  444. count = PAGE_SIZE - 1;
  445. trunc = 1;
  446. }
  447. }
  448. data = kmalloc(count + 1, GFP_KERNEL);
  449. if (data == NULL)
  450. return -ENOMEM;
  451. if (copy_from_user(data, buf, count) != 0) {
  452. rc = -EFAULT;
  453. goto out;
  454. }
  455. /*
  456. * In case of parsing only part of user buf,
  457. * avoid having partial rule at the data buffer
  458. */
  459. if (trunc) {
  460. while (count > 0 && (data[count - 1] != '\n'))
  461. --count;
  462. if (count == 0) {
  463. rc = -EINVAL;
  464. goto out;
  465. }
  466. }
  467. data[count] = '\0';
  468. tokens = (format == SMK_CHANGE_FMT ? 4 : 3);
  469. while (cnt < count) {
  470. if (format == SMK_FIXED24_FMT) {
  471. rc = smk_parse_rule(data, &rule, 1);
  472. if (rc < 0)
  473. goto out;
  474. cnt = count;
  475. } else {
  476. rc = smk_parse_long_rule(data + cnt, &rule, 1, tokens);
  477. if (rc < 0)
  478. goto out;
  479. if (rc == 0) {
  480. rc = -EINVAL;
  481. goto out;
  482. }
  483. cnt += rc;
  484. }
  485. if (rule_list == NULL)
  486. rc = smk_set_access(&rule, &rule.smk_subject->smk_rules,
  487. &rule.smk_subject->smk_rules_lock, 1);
  488. else
  489. rc = smk_set_access(&rule, rule_list, rule_lock, 0);
  490. if (rc)
  491. goto out;
  492. }
  493. rc = cnt;
  494. out:
  495. kfree(data);
  496. return rc;
  497. }
  498. /*
  499. * Core logic for smackfs seq list operations.
  500. */
  501. static void *smk_seq_start(struct seq_file *s, loff_t *pos,
  502. struct list_head *head)
  503. {
  504. struct list_head *list;
  505. int i = *pos;
  506. rcu_read_lock();
  507. for (list = rcu_dereference(list_next_rcu(head));
  508. list != head;
  509. list = rcu_dereference(list_next_rcu(list))) {
  510. if (i-- == 0)
  511. return list;
  512. }
  513. return NULL;
  514. }
  515. static void *smk_seq_next(struct seq_file *s, void *v, loff_t *pos,
  516. struct list_head *head)
  517. {
  518. struct list_head *list = v;
  519. ++*pos;
  520. list = rcu_dereference(list_next_rcu(list));
  521. return (list == head) ? NULL : list;
  522. }
  523. static void smk_seq_stop(struct seq_file *s, void *v)
  524. {
  525. rcu_read_unlock();
  526. }
  527. static void smk_rule_show(struct seq_file *s, struct smack_rule *srp, int max)
  528. {
  529. /*
  530. * Don't show any rules with label names too long for
  531. * interface file (/smack/load or /smack/load2)
  532. * because you should expect to be able to write
  533. * anything you read back.
  534. */
  535. if (strlen(srp->smk_subject->smk_known) >= max ||
  536. strlen(srp->smk_object->smk_known) >= max)
  537. return;
  538. if (srp->smk_access == 0)
  539. return;
  540. seq_printf(s, "%s %s",
  541. srp->smk_subject->smk_known,
  542. srp->smk_object->smk_known);
  543. seq_putc(s, ' ');
  544. if (srp->smk_access & MAY_READ)
  545. seq_putc(s, 'r');
  546. if (srp->smk_access & MAY_WRITE)
  547. seq_putc(s, 'w');
  548. if (srp->smk_access & MAY_EXEC)
  549. seq_putc(s, 'x');
  550. if (srp->smk_access & MAY_APPEND)
  551. seq_putc(s, 'a');
  552. if (srp->smk_access & MAY_TRANSMUTE)
  553. seq_putc(s, 't');
  554. if (srp->smk_access & MAY_LOCK)
  555. seq_putc(s, 'l');
  556. if (srp->smk_access & MAY_BRINGUP)
  557. seq_putc(s, 'b');
  558. seq_putc(s, '\n');
  559. }
  560. /*
  561. * Seq_file read operations for /smack/load
  562. */
  563. static void *load2_seq_start(struct seq_file *s, loff_t *pos)
  564. {
  565. return smk_seq_start(s, pos, &smack_rule_list);
  566. }
  567. static void *load2_seq_next(struct seq_file *s, void *v, loff_t *pos)
  568. {
  569. return smk_seq_next(s, v, pos, &smack_rule_list);
  570. }
  571. static int load_seq_show(struct seq_file *s, void *v)
  572. {
  573. struct list_head *list = v;
  574. struct smack_master_list *smlp =
  575. list_entry_rcu(list, struct smack_master_list, list);
  576. smk_rule_show(s, smlp->smk_rule, SMK_LABELLEN);
  577. return 0;
  578. }
  579. static const struct seq_operations load_seq_ops = {
  580. .start = load2_seq_start,
  581. .next = load2_seq_next,
  582. .show = load_seq_show,
  583. .stop = smk_seq_stop,
  584. };
  585. /**
  586. * smk_open_load - open() for /smack/load
  587. * @inode: inode structure representing file
  588. * @file: "load" file pointer
  589. *
  590. * For reading, use load_seq_* seq_file reading operations.
  591. */
  592. static int smk_open_load(struct inode *inode, struct file *file)
  593. {
  594. return seq_open(file, &load_seq_ops);
  595. }
  596. /**
  597. * smk_write_load - write() for /smack/load
  598. * @file: file pointer, not actually used
  599. * @buf: where to get the data from
  600. * @count: bytes sent
  601. * @ppos: where to start - must be 0
  602. *
  603. */
  604. static ssize_t smk_write_load(struct file *file, const char __user *buf,
  605. size_t count, loff_t *ppos)
  606. {
  607. /*
  608. * Must have privilege.
  609. * No partial writes.
  610. * Enough data must be present.
  611. */
  612. if (!smack_privileged(CAP_MAC_ADMIN))
  613. return -EPERM;
  614. return smk_write_rules_list(file, buf, count, ppos, NULL, NULL,
  615. SMK_FIXED24_FMT);
  616. }
  617. static const struct file_operations smk_load_ops = {
  618. .open = smk_open_load,
  619. .read = seq_read,
  620. .llseek = seq_lseek,
  621. .write = smk_write_load,
  622. .release = seq_release,
  623. };
  624. /**
  625. * smk_cipso_doi - initialize the CIPSO domain
  626. */
  627. static void smk_cipso_doi(void)
  628. {
  629. int rc;
  630. struct cipso_v4_doi *doip;
  631. struct netlbl_audit nai;
  632. smk_netlabel_audit_set(&nai);
  633. rc = netlbl_cfg_map_del(NULL, PF_INET, NULL, NULL, &nai);
  634. if (rc != 0)
  635. printk(KERN_WARNING "%s:%d remove rc = %d\n",
  636. __func__, __LINE__, rc);
  637. doip = kmalloc(sizeof(struct cipso_v4_doi), GFP_KERNEL);
  638. if (doip == NULL)
  639. panic("smack: Failed to initialize cipso DOI.\n");
  640. doip->map.std = NULL;
  641. doip->doi = smk_cipso_doi_value;
  642. doip->type = CIPSO_V4_MAP_PASS;
  643. doip->tags[0] = CIPSO_V4_TAG_RBITMAP;
  644. for (rc = 1; rc < CIPSO_V4_TAG_MAXCNT; rc++)
  645. doip->tags[rc] = CIPSO_V4_TAG_INVALID;
  646. rc = netlbl_cfg_cipsov4_add(doip, &nai);
  647. if (rc != 0) {
  648. printk(KERN_WARNING "%s:%d cipso add rc = %d\n",
  649. __func__, __LINE__, rc);
  650. kfree(doip);
  651. return;
  652. }
  653. rc = netlbl_cfg_cipsov4_map_add(doip->doi, NULL, NULL, NULL, &nai);
  654. if (rc != 0) {
  655. printk(KERN_WARNING "%s:%d map add rc = %d\n",
  656. __func__, __LINE__, rc);
  657. kfree(doip);
  658. return;
  659. }
  660. }
  661. /**
  662. * smk_unlbl_ambient - initialize the unlabeled domain
  663. * @oldambient: previous domain string
  664. */
  665. static void smk_unlbl_ambient(char *oldambient)
  666. {
  667. int rc;
  668. struct netlbl_audit nai;
  669. smk_netlabel_audit_set(&nai);
  670. if (oldambient != NULL) {
  671. rc = netlbl_cfg_map_del(oldambient, PF_INET, NULL, NULL, &nai);
  672. if (rc != 0)
  673. printk(KERN_WARNING "%s:%d remove rc = %d\n",
  674. __func__, __LINE__, rc);
  675. }
  676. if (smack_net_ambient == NULL)
  677. smack_net_ambient = &smack_known_floor;
  678. rc = netlbl_cfg_unlbl_map_add(smack_net_ambient->smk_known, PF_INET,
  679. NULL, NULL, &nai);
  680. if (rc != 0)
  681. printk(KERN_WARNING "%s:%d add rc = %d\n",
  682. __func__, __LINE__, rc);
  683. }
  684. /*
  685. * Seq_file read operations for /smack/cipso
  686. */
  687. static void *cipso_seq_start(struct seq_file *s, loff_t *pos)
  688. {
  689. return smk_seq_start(s, pos, &smack_known_list);
  690. }
  691. static void *cipso_seq_next(struct seq_file *s, void *v, loff_t *pos)
  692. {
  693. return smk_seq_next(s, v, pos, &smack_known_list);
  694. }
  695. /*
  696. * Print cipso labels in format:
  697. * label level[/cat[,cat]]
  698. */
  699. static int cipso_seq_show(struct seq_file *s, void *v)
  700. {
  701. struct list_head *list = v;
  702. struct smack_known *skp =
  703. list_entry_rcu(list, struct smack_known, list);
  704. struct netlbl_lsm_catmap *cmp = skp->smk_netlabel.attr.mls.cat;
  705. char sep = '/';
  706. int i;
  707. /*
  708. * Don't show a label that could not have been set using
  709. * /smack/cipso. This is in support of the notion that
  710. * anything read from /smack/cipso ought to be writeable
  711. * to /smack/cipso.
  712. *
  713. * /smack/cipso2 should be used instead.
  714. */
  715. if (strlen(skp->smk_known) >= SMK_LABELLEN)
  716. return 0;
  717. seq_printf(s, "%s %3d", skp->smk_known, skp->smk_netlabel.attr.mls.lvl);
  718. for (i = netlbl_catmap_walk(cmp, 0); i >= 0;
  719. i = netlbl_catmap_walk(cmp, i + 1)) {
  720. seq_printf(s, "%c%d", sep, i);
  721. sep = ',';
  722. }
  723. seq_putc(s, '\n');
  724. return 0;
  725. }
  726. static const struct seq_operations cipso_seq_ops = {
  727. .start = cipso_seq_start,
  728. .next = cipso_seq_next,
  729. .show = cipso_seq_show,
  730. .stop = smk_seq_stop,
  731. };
  732. /**
  733. * smk_open_cipso - open() for /smack/cipso
  734. * @inode: inode structure representing file
  735. * @file: "cipso" file pointer
  736. *
  737. * Connect our cipso_seq_* operations with /smack/cipso
  738. * file_operations
  739. */
  740. static int smk_open_cipso(struct inode *inode, struct file *file)
  741. {
  742. return seq_open(file, &cipso_seq_ops);
  743. }
  744. /**
  745. * smk_set_cipso - do the work for write() for cipso and cipso2
  746. * @file: file pointer, not actually used
  747. * @buf: where to get the data from
  748. * @count: bytes sent
  749. * @ppos: where to start
  750. * @format: /smack/cipso or /smack/cipso2
  751. *
  752. * Accepts only one cipso rule per write call.
  753. * Returns number of bytes written or error code, as appropriate
  754. */
  755. static ssize_t smk_set_cipso(struct file *file, const char __user *buf,
  756. size_t count, loff_t *ppos, int format)
  757. {
  758. struct smack_known *skp;
  759. struct netlbl_lsm_secattr ncats;
  760. char mapcatset[SMK_CIPSOLEN];
  761. int maplevel;
  762. unsigned int cat;
  763. int catlen;
  764. ssize_t rc = -EINVAL;
  765. char *data = NULL;
  766. char *rule;
  767. int ret;
  768. int i;
  769. /*
  770. * Must have privilege.
  771. * No partial writes.
  772. * Enough data must be present.
  773. */
  774. if (!smack_privileged(CAP_MAC_ADMIN))
  775. return -EPERM;
  776. if (*ppos != 0)
  777. return -EINVAL;
  778. if (format == SMK_FIXED24_FMT &&
  779. (count < SMK_CIPSOMIN || count > SMK_CIPSOMAX))
  780. return -EINVAL;
  781. data = kzalloc(count + 1, GFP_KERNEL);
  782. if (data == NULL)
  783. return -ENOMEM;
  784. if (copy_from_user(data, buf, count) != 0) {
  785. rc = -EFAULT;
  786. goto unlockedout;
  787. }
  788. data[count] = '\0';
  789. rule = data;
  790. /*
  791. * Only allow one writer at a time. Writes should be
  792. * quite rare and small in any case.
  793. */
  794. mutex_lock(&smack_cipso_lock);
  795. skp = smk_import_entry(rule, 0);
  796. if (IS_ERR(skp)) {
  797. rc = PTR_ERR(skp);
  798. goto out;
  799. }
  800. if (format == SMK_FIXED24_FMT)
  801. rule += SMK_LABELLEN;
  802. else
  803. rule += strlen(skp->smk_known) + 1;
  804. ret = sscanf(rule, "%d", &maplevel);
  805. if (ret != 1 || maplevel > SMACK_CIPSO_MAXLEVEL)
  806. goto out;
  807. rule += SMK_DIGITLEN;
  808. ret = sscanf(rule, "%d", &catlen);
  809. if (ret != 1 || catlen > SMACK_CIPSO_MAXCATNUM)
  810. goto out;
  811. if (format == SMK_FIXED24_FMT &&
  812. count != (SMK_CIPSOMIN + catlen * SMK_DIGITLEN))
  813. goto out;
  814. memset(mapcatset, 0, sizeof(mapcatset));
  815. for (i = 0; i < catlen; i++) {
  816. rule += SMK_DIGITLEN;
  817. ret = sscanf(rule, "%u", &cat);
  818. if (ret != 1 || cat > SMACK_CIPSO_MAXCATNUM)
  819. goto out;
  820. smack_catset_bit(cat, mapcatset);
  821. }
  822. rc = smk_netlbl_mls(maplevel, mapcatset, &ncats, SMK_CIPSOLEN);
  823. if (rc >= 0) {
  824. netlbl_catmap_free(skp->smk_netlabel.attr.mls.cat);
  825. skp->smk_netlabel.attr.mls.cat = ncats.attr.mls.cat;
  826. skp->smk_netlabel.attr.mls.lvl = ncats.attr.mls.lvl;
  827. rc = count;
  828. }
  829. out:
  830. mutex_unlock(&smack_cipso_lock);
  831. unlockedout:
  832. kfree(data);
  833. return rc;
  834. }
  835. /**
  836. * smk_write_cipso - write() for /smack/cipso
  837. * @file: file pointer, not actually used
  838. * @buf: where to get the data from
  839. * @count: bytes sent
  840. * @ppos: where to start
  841. *
  842. * Accepts only one cipso rule per write call.
  843. * Returns number of bytes written or error code, as appropriate
  844. */
  845. static ssize_t smk_write_cipso(struct file *file, const char __user *buf,
  846. size_t count, loff_t *ppos)
  847. {
  848. return smk_set_cipso(file, buf, count, ppos, SMK_FIXED24_FMT);
  849. }
  850. static const struct file_operations smk_cipso_ops = {
  851. .open = smk_open_cipso,
  852. .read = seq_read,
  853. .llseek = seq_lseek,
  854. .write = smk_write_cipso,
  855. .release = seq_release,
  856. };
  857. /*
  858. * Seq_file read operations for /smack/cipso2
  859. */
  860. /*
  861. * Print cipso labels in format:
  862. * label level[/cat[,cat]]
  863. */
  864. static int cipso2_seq_show(struct seq_file *s, void *v)
  865. {
  866. struct list_head *list = v;
  867. struct smack_known *skp =
  868. list_entry_rcu(list, struct smack_known, list);
  869. struct netlbl_lsm_catmap *cmp = skp->smk_netlabel.attr.mls.cat;
  870. char sep = '/';
  871. int i;
  872. seq_printf(s, "%s %3d", skp->smk_known, skp->smk_netlabel.attr.mls.lvl);
  873. for (i = netlbl_catmap_walk(cmp, 0); i >= 0;
  874. i = netlbl_catmap_walk(cmp, i + 1)) {
  875. seq_printf(s, "%c%d", sep, i);
  876. sep = ',';
  877. }
  878. seq_putc(s, '\n');
  879. return 0;
  880. }
  881. static const struct seq_operations cipso2_seq_ops = {
  882. .start = cipso_seq_start,
  883. .next = cipso_seq_next,
  884. .show = cipso2_seq_show,
  885. .stop = smk_seq_stop,
  886. };
  887. /**
  888. * smk_open_cipso2 - open() for /smack/cipso2
  889. * @inode: inode structure representing file
  890. * @file: "cipso2" file pointer
  891. *
  892. * Connect our cipso_seq_* operations with /smack/cipso2
  893. * file_operations
  894. */
  895. static int smk_open_cipso2(struct inode *inode, struct file *file)
  896. {
  897. return seq_open(file, &cipso2_seq_ops);
  898. }
  899. /**
  900. * smk_write_cipso2 - write() for /smack/cipso2
  901. * @file: file pointer, not actually used
  902. * @buf: where to get the data from
  903. * @count: bytes sent
  904. * @ppos: where to start
  905. *
  906. * Accepts only one cipso rule per write call.
  907. * Returns number of bytes written or error code, as appropriate
  908. */
  909. static ssize_t smk_write_cipso2(struct file *file, const char __user *buf,
  910. size_t count, loff_t *ppos)
  911. {
  912. return smk_set_cipso(file, buf, count, ppos, SMK_LONG_FMT);
  913. }
  914. static const struct file_operations smk_cipso2_ops = {
  915. .open = smk_open_cipso2,
  916. .read = seq_read,
  917. .llseek = seq_lseek,
  918. .write = smk_write_cipso2,
  919. .release = seq_release,
  920. };
  921. /*
  922. * Seq_file read operations for /smack/netlabel
  923. */
  924. static void *net4addr_seq_start(struct seq_file *s, loff_t *pos)
  925. {
  926. return smk_seq_start(s, pos, &smk_net4addr_list);
  927. }
  928. static void *net4addr_seq_next(struct seq_file *s, void *v, loff_t *pos)
  929. {
  930. return smk_seq_next(s, v, pos, &smk_net4addr_list);
  931. }
  932. /*
  933. * Print host/label pairs
  934. */
  935. static int net4addr_seq_show(struct seq_file *s, void *v)
  936. {
  937. struct list_head *list = v;
  938. struct smk_net4addr *skp =
  939. list_entry_rcu(list, struct smk_net4addr, list);
  940. char *kp = SMACK_CIPSO_OPTION;
  941. if (skp->smk_label != NULL)
  942. kp = skp->smk_label->smk_known;
  943. seq_printf(s, "%pI4/%d %s\n", &skp->smk_host.s_addr,
  944. skp->smk_masks, kp);
  945. return 0;
  946. }
  947. static const struct seq_operations net4addr_seq_ops = {
  948. .start = net4addr_seq_start,
  949. .next = net4addr_seq_next,
  950. .show = net4addr_seq_show,
  951. .stop = smk_seq_stop,
  952. };
  953. /**
  954. * smk_open_net4addr - open() for /smack/netlabel
  955. * @inode: inode structure representing file
  956. * @file: "netlabel" file pointer
  957. *
  958. * Connect our net4addr_seq_* operations with /smack/netlabel
  959. * file_operations
  960. */
  961. static int smk_open_net4addr(struct inode *inode, struct file *file)
  962. {
  963. return seq_open(file, &net4addr_seq_ops);
  964. }
  965. /**
  966. * smk_net4addr_insert
  967. * @new : netlabel to insert
  968. *
  969. * This helper insert netlabel in the smack_net4addrs list
  970. * sorted by netmask length (longest to smallest)
  971. * locked by &smk_net4addr_lock in smk_write_net4addr
  972. *
  973. */
  974. static void smk_net4addr_insert(struct smk_net4addr *new)
  975. {
  976. struct smk_net4addr *m;
  977. struct smk_net4addr *m_next;
  978. if (list_empty(&smk_net4addr_list)) {
  979. list_add_rcu(&new->list, &smk_net4addr_list);
  980. return;
  981. }
  982. m = list_entry_rcu(smk_net4addr_list.next,
  983. struct smk_net4addr, list);
  984. /* the comparison '>' is a bit hacky, but works */
  985. if (new->smk_masks > m->smk_masks) {
  986. list_add_rcu(&new->list, &smk_net4addr_list);
  987. return;
  988. }
  989. list_for_each_entry_rcu(m, &smk_net4addr_list, list) {
  990. if (list_is_last(&m->list, &smk_net4addr_list)) {
  991. list_add_rcu(&new->list, &m->list);
  992. return;
  993. }
  994. m_next = list_entry_rcu(m->list.next,
  995. struct smk_net4addr, list);
  996. if (new->smk_masks > m_next->smk_masks) {
  997. list_add_rcu(&new->list, &m->list);
  998. return;
  999. }
  1000. }
  1001. }
  1002. /**
  1003. * smk_write_net4addr - write() for /smack/netlabel
  1004. * @file: file pointer, not actually used
  1005. * @buf: where to get the data from
  1006. * @count: bytes sent
  1007. * @ppos: where to start
  1008. *
  1009. * Accepts only one net4addr per write call.
  1010. * Returns number of bytes written or error code, as appropriate
  1011. */
  1012. static ssize_t smk_write_net4addr(struct file *file, const char __user *buf,
  1013. size_t count, loff_t *ppos)
  1014. {
  1015. struct smk_net4addr *snp;
  1016. struct sockaddr_in newname;
  1017. char *smack;
  1018. struct smack_known *skp = NULL;
  1019. char *data;
  1020. char *host = (char *)&newname.sin_addr.s_addr;
  1021. int rc;
  1022. struct netlbl_audit audit_info;
  1023. struct in_addr mask;
  1024. unsigned int m;
  1025. unsigned int masks;
  1026. int found;
  1027. u32 mask_bits = (1<<31);
  1028. __be32 nsa;
  1029. u32 temp_mask;
  1030. /*
  1031. * Must have privilege.
  1032. * No partial writes.
  1033. * Enough data must be present.
  1034. * "<addr/mask, as a.b.c.d/e><space><label>"
  1035. * "<addr, as a.b.c.d><space><label>"
  1036. */
  1037. if (!smack_privileged(CAP_MAC_ADMIN))
  1038. return -EPERM;
  1039. if (*ppos != 0)
  1040. return -EINVAL;
  1041. if (count < SMK_NETLBLADDRMIN)
  1042. return -EINVAL;
  1043. data = kzalloc(count + 1, GFP_KERNEL);
  1044. if (data == NULL)
  1045. return -ENOMEM;
  1046. if (copy_from_user(data, buf, count) != 0) {
  1047. rc = -EFAULT;
  1048. goto free_data_out;
  1049. }
  1050. smack = kzalloc(count + 1, GFP_KERNEL);
  1051. if (smack == NULL) {
  1052. rc = -ENOMEM;
  1053. goto free_data_out;
  1054. }
  1055. data[count] = '\0';
  1056. rc = sscanf(data, "%hhd.%hhd.%hhd.%hhd/%u %s",
  1057. &host[0], &host[1], &host[2], &host[3], &masks, smack);
  1058. if (rc != 6) {
  1059. rc = sscanf(data, "%hhd.%hhd.%hhd.%hhd %s",
  1060. &host[0], &host[1], &host[2], &host[3], smack);
  1061. if (rc != 5) {
  1062. rc = -EINVAL;
  1063. goto free_out;
  1064. }
  1065. m = BEBITS;
  1066. masks = 32;
  1067. }
  1068. if (masks > BEBITS) {
  1069. rc = -EINVAL;
  1070. goto free_out;
  1071. }
  1072. /*
  1073. * If smack begins with '-', it is an option, don't import it
  1074. */
  1075. if (smack[0] != '-') {
  1076. skp = smk_import_entry(smack, 0);
  1077. if (IS_ERR(skp)) {
  1078. rc = PTR_ERR(skp);
  1079. goto free_out;
  1080. }
  1081. } else {
  1082. /*
  1083. * Only the -CIPSO option is supported for IPv4
  1084. */
  1085. if (strcmp(smack, SMACK_CIPSO_OPTION) != 0) {
  1086. rc = -EINVAL;
  1087. goto free_out;
  1088. }
  1089. }
  1090. for (m = masks, temp_mask = 0; m > 0; m--) {
  1091. temp_mask |= mask_bits;
  1092. mask_bits >>= 1;
  1093. }
  1094. mask.s_addr = cpu_to_be32(temp_mask);
  1095. newname.sin_addr.s_addr &= mask.s_addr;
  1096. /*
  1097. * Only allow one writer at a time. Writes should be
  1098. * quite rare and small in any case.
  1099. */
  1100. mutex_lock(&smk_net4addr_lock);
  1101. nsa = newname.sin_addr.s_addr;
  1102. /* try to find if the prefix is already in the list */
  1103. found = 0;
  1104. list_for_each_entry_rcu(snp, &smk_net4addr_list, list) {
  1105. if (snp->smk_host.s_addr == nsa && snp->smk_masks == masks) {
  1106. found = 1;
  1107. break;
  1108. }
  1109. }
  1110. smk_netlabel_audit_set(&audit_info);
  1111. if (found == 0) {
  1112. snp = kzalloc(sizeof(*snp), GFP_KERNEL);
  1113. if (snp == NULL)
  1114. rc = -ENOMEM;
  1115. else {
  1116. rc = 0;
  1117. snp->smk_host.s_addr = newname.sin_addr.s_addr;
  1118. snp->smk_mask.s_addr = mask.s_addr;
  1119. snp->smk_label = skp;
  1120. snp->smk_masks = masks;
  1121. smk_net4addr_insert(snp);
  1122. }
  1123. } else {
  1124. /*
  1125. * Delete the unlabeled entry, only if the previous label
  1126. * wasn't the special CIPSO option
  1127. */
  1128. if (snp->smk_label != NULL)
  1129. rc = netlbl_cfg_unlbl_static_del(&init_net, NULL,
  1130. &snp->smk_host, &snp->smk_mask,
  1131. PF_INET, &audit_info);
  1132. else
  1133. rc = 0;
  1134. snp->smk_label = skp;
  1135. }
  1136. /*
  1137. * Now tell netlabel about the single label nature of
  1138. * this host so that incoming packets get labeled.
  1139. * but only if we didn't get the special CIPSO option
  1140. */
  1141. if (rc == 0 && skp != NULL)
  1142. rc = netlbl_cfg_unlbl_static_add(&init_net, NULL,
  1143. &snp->smk_host, &snp->smk_mask, PF_INET,
  1144. snp->smk_label->smk_secid, &audit_info);
  1145. if (rc == 0)
  1146. rc = count;
  1147. mutex_unlock(&smk_net4addr_lock);
  1148. free_out:
  1149. kfree(smack);
  1150. free_data_out:
  1151. kfree(data);
  1152. return rc;
  1153. }
  1154. static const struct file_operations smk_net4addr_ops = {
  1155. .open = smk_open_net4addr,
  1156. .read = seq_read,
  1157. .llseek = seq_lseek,
  1158. .write = smk_write_net4addr,
  1159. .release = seq_release,
  1160. };
  1161. #if IS_ENABLED(CONFIG_IPV6)
  1162. /*
  1163. * Seq_file read operations for /smack/netlabel6
  1164. */
  1165. static void *net6addr_seq_start(struct seq_file *s, loff_t *pos)
  1166. {
  1167. return smk_seq_start(s, pos, &smk_net6addr_list);
  1168. }
  1169. static void *net6addr_seq_next(struct seq_file *s, void *v, loff_t *pos)
  1170. {
  1171. return smk_seq_next(s, v, pos, &smk_net6addr_list);
  1172. }
  1173. /*
  1174. * Print host/label pairs
  1175. */
  1176. static int net6addr_seq_show(struct seq_file *s, void *v)
  1177. {
  1178. struct list_head *list = v;
  1179. struct smk_net6addr *skp =
  1180. list_entry(list, struct smk_net6addr, list);
  1181. if (skp->smk_label != NULL)
  1182. seq_printf(s, "%pI6/%d %s\n", &skp->smk_host, skp->smk_masks,
  1183. skp->smk_label->smk_known);
  1184. return 0;
  1185. }
  1186. static const struct seq_operations net6addr_seq_ops = {
  1187. .start = net6addr_seq_start,
  1188. .next = net6addr_seq_next,
  1189. .show = net6addr_seq_show,
  1190. .stop = smk_seq_stop,
  1191. };
  1192. /**
  1193. * smk_open_net6addr - open() for /smack/netlabel
  1194. * @inode: inode structure representing file
  1195. * @file: "netlabel" file pointer
  1196. *
  1197. * Connect our net6addr_seq_* operations with /smack/netlabel
  1198. * file_operations
  1199. */
  1200. static int smk_open_net6addr(struct inode *inode, struct file *file)
  1201. {
  1202. return seq_open(file, &net6addr_seq_ops);
  1203. }
  1204. /**
  1205. * smk_net6addr_insert
  1206. * @new : entry to insert
  1207. *
  1208. * This inserts an entry in the smack_net6addrs list
  1209. * sorted by netmask length (longest to smallest)
  1210. * locked by &smk_net6addr_lock in smk_write_net6addr
  1211. *
  1212. */
  1213. static void smk_net6addr_insert(struct smk_net6addr *new)
  1214. {
  1215. struct smk_net6addr *m_next;
  1216. struct smk_net6addr *m;
  1217. if (list_empty(&smk_net6addr_list)) {
  1218. list_add_rcu(&new->list, &smk_net6addr_list);
  1219. return;
  1220. }
  1221. m = list_entry_rcu(smk_net6addr_list.next,
  1222. struct smk_net6addr, list);
  1223. if (new->smk_masks > m->smk_masks) {
  1224. list_add_rcu(&new->list, &smk_net6addr_list);
  1225. return;
  1226. }
  1227. list_for_each_entry_rcu(m, &smk_net6addr_list, list) {
  1228. if (list_is_last(&m->list, &smk_net6addr_list)) {
  1229. list_add_rcu(&new->list, &m->list);
  1230. return;
  1231. }
  1232. m_next = list_entry_rcu(m->list.next,
  1233. struct smk_net6addr, list);
  1234. if (new->smk_masks > m_next->smk_masks) {
  1235. list_add_rcu(&new->list, &m->list);
  1236. return;
  1237. }
  1238. }
  1239. }
  1240. /**
  1241. * smk_write_net6addr - write() for /smack/netlabel
  1242. * @file: file pointer, not actually used
  1243. * @buf: where to get the data from
  1244. * @count: bytes sent
  1245. * @ppos: where to start
  1246. *
  1247. * Accepts only one net6addr per write call.
  1248. * Returns number of bytes written or error code, as appropriate
  1249. */
  1250. static ssize_t smk_write_net6addr(struct file *file, const char __user *buf,
  1251. size_t count, loff_t *ppos)
  1252. {
  1253. struct smk_net6addr *snp;
  1254. struct in6_addr newname;
  1255. struct in6_addr fullmask;
  1256. struct smack_known *skp = NULL;
  1257. char *smack;
  1258. char *data;
  1259. int rc = 0;
  1260. int found = 0;
  1261. int i;
  1262. unsigned int scanned[8];
  1263. unsigned int m;
  1264. unsigned int mask = 128;
  1265. /*
  1266. * Must have privilege.
  1267. * No partial writes.
  1268. * Enough data must be present.
  1269. * "<addr/mask, as a:b:c:d:e:f:g:h/e><space><label>"
  1270. * "<addr, as a:b:c:d:e:f:g:h><space><label>"
  1271. */
  1272. if (!smack_privileged(CAP_MAC_ADMIN))
  1273. return -EPERM;
  1274. if (*ppos != 0)
  1275. return -EINVAL;
  1276. if (count < SMK_NETLBLADDRMIN)
  1277. return -EINVAL;
  1278. data = kzalloc(count + 1, GFP_KERNEL);
  1279. if (data == NULL)
  1280. return -ENOMEM;
  1281. if (copy_from_user(data, buf, count) != 0) {
  1282. rc = -EFAULT;
  1283. goto free_data_out;
  1284. }
  1285. smack = kzalloc(count + 1, GFP_KERNEL);
  1286. if (smack == NULL) {
  1287. rc = -ENOMEM;
  1288. goto free_data_out;
  1289. }
  1290. data[count] = '\0';
  1291. i = sscanf(data, "%x:%x:%x:%x:%x:%x:%x:%x/%u %s",
  1292. &scanned[0], &scanned[1], &scanned[2], &scanned[3],
  1293. &scanned[4], &scanned[5], &scanned[6], &scanned[7],
  1294. &mask, smack);
  1295. if (i != 10) {
  1296. i = sscanf(data, "%x:%x:%x:%x:%x:%x:%x:%x %s",
  1297. &scanned[0], &scanned[1], &scanned[2],
  1298. &scanned[3], &scanned[4], &scanned[5],
  1299. &scanned[6], &scanned[7], smack);
  1300. if (i != 9) {
  1301. rc = -EINVAL;
  1302. goto free_out;
  1303. }
  1304. }
  1305. if (mask > 128) {
  1306. rc = -EINVAL;
  1307. goto free_out;
  1308. }
  1309. for (i = 0; i < 8; i++) {
  1310. if (scanned[i] > 0xffff) {
  1311. rc = -EINVAL;
  1312. goto free_out;
  1313. }
  1314. newname.s6_addr16[i] = htons(scanned[i]);
  1315. }
  1316. /*
  1317. * If smack begins with '-', it is an option, don't import it
  1318. */
  1319. if (smack[0] != '-') {
  1320. skp = smk_import_entry(smack, 0);
  1321. if (skp == NULL) {
  1322. rc = -EINVAL;
  1323. goto free_out;
  1324. }
  1325. } else {
  1326. /*
  1327. * Only -DELETE is supported for IPv6
  1328. */
  1329. if (strcmp(smack, SMACK_DELETE_OPTION) != 0) {
  1330. rc = -EINVAL;
  1331. goto free_out;
  1332. }
  1333. }
  1334. for (i = 0, m = mask; i < 8; i++) {
  1335. if (m >= 16) {
  1336. fullmask.s6_addr16[i] = 0xffff;
  1337. m -= 16;
  1338. } else if (m > 0) {
  1339. fullmask.s6_addr16[i] = (1 << m) - 1;
  1340. m = 0;
  1341. } else
  1342. fullmask.s6_addr16[i] = 0;
  1343. newname.s6_addr16[i] &= fullmask.s6_addr16[i];
  1344. }
  1345. /*
  1346. * Only allow one writer at a time. Writes should be
  1347. * quite rare and small in any case.
  1348. */
  1349. mutex_lock(&smk_net6addr_lock);
  1350. /*
  1351. * Try to find the prefix in the list
  1352. */
  1353. list_for_each_entry_rcu(snp, &smk_net6addr_list, list) {
  1354. if (mask != snp->smk_masks)
  1355. continue;
  1356. for (found = 1, i = 0; i < 8; i++) {
  1357. if (newname.s6_addr16[i] !=
  1358. snp->smk_host.s6_addr16[i]) {
  1359. found = 0;
  1360. break;
  1361. }
  1362. }
  1363. if (found == 1)
  1364. break;
  1365. }
  1366. if (found == 0) {
  1367. snp = kzalloc(sizeof(*snp), GFP_KERNEL);
  1368. if (snp == NULL)
  1369. rc = -ENOMEM;
  1370. else {
  1371. snp->smk_host = newname;
  1372. snp->smk_mask = fullmask;
  1373. snp->smk_masks = mask;
  1374. snp->smk_label = skp;
  1375. smk_net6addr_insert(snp);
  1376. }
  1377. } else {
  1378. snp->smk_label = skp;
  1379. }
  1380. if (rc == 0)
  1381. rc = count;
  1382. mutex_unlock(&smk_net6addr_lock);
  1383. free_out:
  1384. kfree(smack);
  1385. free_data_out:
  1386. kfree(data);
  1387. return rc;
  1388. }
  1389. static const struct file_operations smk_net6addr_ops = {
  1390. .open = smk_open_net6addr,
  1391. .read = seq_read,
  1392. .llseek = seq_lseek,
  1393. .write = smk_write_net6addr,
  1394. .release = seq_release,
  1395. };
  1396. #endif /* CONFIG_IPV6 */
  1397. /**
  1398. * smk_read_doi - read() for /smack/doi
  1399. * @filp: file pointer, not actually used
  1400. * @buf: where to put the result
  1401. * @count: maximum to send along
  1402. * @ppos: where to start
  1403. *
  1404. * Returns number of bytes read or error code, as appropriate
  1405. */
  1406. static ssize_t smk_read_doi(struct file *filp, char __user *buf,
  1407. size_t count, loff_t *ppos)
  1408. {
  1409. char temp[80];
  1410. ssize_t rc;
  1411. if (*ppos != 0)
  1412. return 0;
  1413. sprintf(temp, "%d", smk_cipso_doi_value);
  1414. rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
  1415. return rc;
  1416. }
  1417. /**
  1418. * smk_write_doi - write() for /smack/doi
  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_doi(struct file *file, const char __user *buf,
  1427. size_t count, loff_t *ppos)
  1428. {
  1429. char temp[80];
  1430. int i;
  1431. if (!smack_privileged(CAP_MAC_ADMIN))
  1432. return -EPERM;
  1433. if (count >= sizeof(temp) || count == 0)
  1434. return -EINVAL;
  1435. if (copy_from_user(temp, buf, count) != 0)
  1436. return -EFAULT;
  1437. temp[count] = '\0';
  1438. if (sscanf(temp, "%d", &i) != 1)
  1439. return -EINVAL;
  1440. smk_cipso_doi_value = i;
  1441. smk_cipso_doi();
  1442. return count;
  1443. }
  1444. static const struct file_operations smk_doi_ops = {
  1445. .read = smk_read_doi,
  1446. .write = smk_write_doi,
  1447. .llseek = default_llseek,
  1448. };
  1449. /**
  1450. * smk_read_direct - read() for /smack/direct
  1451. * @filp: file pointer, not actually used
  1452. * @buf: where to put the result
  1453. * @count: maximum to send along
  1454. * @ppos: where to start
  1455. *
  1456. * Returns number of bytes read or error code, as appropriate
  1457. */
  1458. static ssize_t smk_read_direct(struct file *filp, char __user *buf,
  1459. size_t count, loff_t *ppos)
  1460. {
  1461. char temp[80];
  1462. ssize_t rc;
  1463. if (*ppos != 0)
  1464. return 0;
  1465. sprintf(temp, "%d", smack_cipso_direct);
  1466. rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
  1467. return rc;
  1468. }
  1469. /**
  1470. * smk_write_direct - write() for /smack/direct
  1471. * @file: file pointer, not actually used
  1472. * @buf: where to get the data from
  1473. * @count: bytes sent
  1474. * @ppos: where to start
  1475. *
  1476. * Returns number of bytes written or error code, as appropriate
  1477. */
  1478. static ssize_t smk_write_direct(struct file *file, const char __user *buf,
  1479. size_t count, loff_t *ppos)
  1480. {
  1481. struct smack_known *skp;
  1482. char temp[80];
  1483. int i;
  1484. if (!smack_privileged(CAP_MAC_ADMIN))
  1485. return -EPERM;
  1486. if (count >= sizeof(temp) || count == 0)
  1487. return -EINVAL;
  1488. if (copy_from_user(temp, buf, count) != 0)
  1489. return -EFAULT;
  1490. temp[count] = '\0';
  1491. if (sscanf(temp, "%d", &i) != 1)
  1492. return -EINVAL;
  1493. /*
  1494. * Don't do anything if the value hasn't actually changed.
  1495. * If it is changing reset the level on entries that were
  1496. * set up to be direct when they were created.
  1497. */
  1498. if (smack_cipso_direct != i) {
  1499. mutex_lock(&smack_known_lock);
  1500. list_for_each_entry_rcu(skp, &smack_known_list, list)
  1501. if (skp->smk_netlabel.attr.mls.lvl ==
  1502. smack_cipso_direct)
  1503. skp->smk_netlabel.attr.mls.lvl = i;
  1504. smack_cipso_direct = i;
  1505. mutex_unlock(&smack_known_lock);
  1506. }
  1507. return count;
  1508. }
  1509. static const struct file_operations smk_direct_ops = {
  1510. .read = smk_read_direct,
  1511. .write = smk_write_direct,
  1512. .llseek = default_llseek,
  1513. };
  1514. /**
  1515. * smk_read_mapped - read() for /smack/mapped
  1516. * @filp: file pointer, not actually used
  1517. * @buf: where to put the result
  1518. * @count: maximum to send along
  1519. * @ppos: where to start
  1520. *
  1521. * Returns number of bytes read or error code, as appropriate
  1522. */
  1523. static ssize_t smk_read_mapped(struct file *filp, char __user *buf,
  1524. size_t count, loff_t *ppos)
  1525. {
  1526. char temp[80];
  1527. ssize_t rc;
  1528. if (*ppos != 0)
  1529. return 0;
  1530. sprintf(temp, "%d", smack_cipso_mapped);
  1531. rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
  1532. return rc;
  1533. }
  1534. /**
  1535. * smk_write_mapped - write() for /smack/mapped
  1536. * @file: file pointer, not actually used
  1537. * @buf: where to get the data from
  1538. * @count: bytes sent
  1539. * @ppos: where to start
  1540. *
  1541. * Returns number of bytes written or error code, as appropriate
  1542. */
  1543. static ssize_t smk_write_mapped(struct file *file, const char __user *buf,
  1544. size_t count, loff_t *ppos)
  1545. {
  1546. struct smack_known *skp;
  1547. char temp[80];
  1548. int i;
  1549. if (!smack_privileged(CAP_MAC_ADMIN))
  1550. return -EPERM;
  1551. if (count >= sizeof(temp) || count == 0)
  1552. return -EINVAL;
  1553. if (copy_from_user(temp, buf, count) != 0)
  1554. return -EFAULT;
  1555. temp[count] = '\0';
  1556. if (sscanf(temp, "%d", &i) != 1)
  1557. return -EINVAL;
  1558. /*
  1559. * Don't do anything if the value hasn't actually changed.
  1560. * If it is changing reset the level on entries that were
  1561. * set up to be mapped when they were created.
  1562. */
  1563. if (smack_cipso_mapped != i) {
  1564. mutex_lock(&smack_known_lock);
  1565. list_for_each_entry_rcu(skp, &smack_known_list, list)
  1566. if (skp->smk_netlabel.attr.mls.lvl ==
  1567. smack_cipso_mapped)
  1568. skp->smk_netlabel.attr.mls.lvl = i;
  1569. smack_cipso_mapped = i;
  1570. mutex_unlock(&smack_known_lock);
  1571. }
  1572. return count;
  1573. }
  1574. static const struct file_operations smk_mapped_ops = {
  1575. .read = smk_read_mapped,
  1576. .write = smk_write_mapped,
  1577. .llseek = default_llseek,
  1578. };
  1579. /**
  1580. * smk_read_ambient - read() for /smack/ambient
  1581. * @filp: file pointer, not actually used
  1582. * @buf: where to put the result
  1583. * @cn: maximum to send along
  1584. * @ppos: where to start
  1585. *
  1586. * Returns number of bytes read or error code, as appropriate
  1587. */
  1588. static ssize_t smk_read_ambient(struct file *filp, char __user *buf,
  1589. size_t cn, loff_t *ppos)
  1590. {
  1591. ssize_t rc;
  1592. int asize;
  1593. if (*ppos != 0)
  1594. return 0;
  1595. /*
  1596. * Being careful to avoid a problem in the case where
  1597. * smack_net_ambient gets changed in midstream.
  1598. */
  1599. mutex_lock(&smack_ambient_lock);
  1600. asize = strlen(smack_net_ambient->smk_known) + 1;
  1601. if (cn >= asize)
  1602. rc = simple_read_from_buffer(buf, cn, ppos,
  1603. smack_net_ambient->smk_known,
  1604. asize);
  1605. else
  1606. rc = -EINVAL;
  1607. mutex_unlock(&smack_ambient_lock);
  1608. return rc;
  1609. }
  1610. /**
  1611. * smk_write_ambient - write() for /smack/ambient
  1612. * @file: file pointer, not actually used
  1613. * @buf: where to get the data from
  1614. * @count: bytes sent
  1615. * @ppos: where to start
  1616. *
  1617. * Returns number of bytes written or error code, as appropriate
  1618. */
  1619. static ssize_t smk_write_ambient(struct file *file, const char __user *buf,
  1620. size_t count, loff_t *ppos)
  1621. {
  1622. struct smack_known *skp;
  1623. char *oldambient;
  1624. char *data;
  1625. int rc = count;
  1626. if (!smack_privileged(CAP_MAC_ADMIN))
  1627. return -EPERM;
  1628. data = kzalloc(count + 1, GFP_KERNEL);
  1629. if (data == NULL)
  1630. return -ENOMEM;
  1631. if (copy_from_user(data, buf, count) != 0) {
  1632. rc = -EFAULT;
  1633. goto out;
  1634. }
  1635. skp = smk_import_entry(data, count);
  1636. if (IS_ERR(skp)) {
  1637. rc = PTR_ERR(skp);
  1638. goto out;
  1639. }
  1640. mutex_lock(&smack_ambient_lock);
  1641. oldambient = smack_net_ambient->smk_known;
  1642. smack_net_ambient = skp;
  1643. smk_unlbl_ambient(oldambient);
  1644. mutex_unlock(&smack_ambient_lock);
  1645. out:
  1646. kfree(data);
  1647. return rc;
  1648. }
  1649. static const struct file_operations smk_ambient_ops = {
  1650. .read = smk_read_ambient,
  1651. .write = smk_write_ambient,
  1652. .llseek = default_llseek,
  1653. };
  1654. /*
  1655. * Seq_file operations for /smack/onlycap
  1656. */
  1657. static void *onlycap_seq_start(struct seq_file *s, loff_t *pos)
  1658. {
  1659. return smk_seq_start(s, pos, &smack_onlycap_list);
  1660. }
  1661. static void *onlycap_seq_next(struct seq_file *s, void *v, loff_t *pos)
  1662. {
  1663. return smk_seq_next(s, v, pos, &smack_onlycap_list);
  1664. }
  1665. static int onlycap_seq_show(struct seq_file *s, void *v)
  1666. {
  1667. struct list_head *list = v;
  1668. struct smack_onlycap *sop =
  1669. list_entry_rcu(list, struct smack_onlycap, list);
  1670. seq_puts(s, sop->smk_label->smk_known);
  1671. seq_putc(s, ' ');
  1672. return 0;
  1673. }
  1674. static const struct seq_operations onlycap_seq_ops = {
  1675. .start = onlycap_seq_start,
  1676. .next = onlycap_seq_next,
  1677. .show = onlycap_seq_show,
  1678. .stop = smk_seq_stop,
  1679. };
  1680. static int smk_open_onlycap(struct inode *inode, struct file *file)
  1681. {
  1682. return seq_open(file, &onlycap_seq_ops);
  1683. }
  1684. /**
  1685. * smk_list_swap_rcu - swap public list with a private one in RCU-safe way
  1686. * The caller must hold appropriate mutex to prevent concurrent modifications
  1687. * to the public list.
  1688. * Private list is assumed to be not accessible to other threads yet.
  1689. *
  1690. * @public: public list
  1691. * @private: private list
  1692. */
  1693. static void smk_list_swap_rcu(struct list_head *public,
  1694. struct list_head *private)
  1695. {
  1696. struct list_head *first, *last;
  1697. if (list_empty(public)) {
  1698. list_splice_init_rcu(private, public, synchronize_rcu);
  1699. } else {
  1700. /* Remember public list before replacing it */
  1701. first = public->next;
  1702. last = public->prev;
  1703. /* Publish private list in place of public in RCU-safe way */
  1704. private->prev->next = public;
  1705. private->next->prev = public;
  1706. rcu_assign_pointer(public->next, private->next);
  1707. public->prev = private->prev;
  1708. synchronize_rcu();
  1709. /* When all readers are done with the old public list,
  1710. * attach it in place of private */
  1711. private->next = first;
  1712. private->prev = last;
  1713. first->prev = private;
  1714. last->next = private;
  1715. }
  1716. }
  1717. /**
  1718. * smk_write_onlycap - write() for smackfs/onlycap
  1719. * @file: file pointer, not actually used
  1720. * @buf: where to get the data from
  1721. * @count: bytes sent
  1722. * @ppos: where to start
  1723. *
  1724. * Returns number of bytes written or error code, as appropriate
  1725. */
  1726. static ssize_t smk_write_onlycap(struct file *file, const char __user *buf,
  1727. size_t count, loff_t *ppos)
  1728. {
  1729. char *data;
  1730. char *data_parse;
  1731. char *tok;
  1732. struct smack_known *skp;
  1733. struct smack_onlycap *sop;
  1734. struct smack_onlycap *sop2;
  1735. LIST_HEAD(list_tmp);
  1736. int rc = count;
  1737. if (!smack_privileged(CAP_MAC_ADMIN))
  1738. return -EPERM;
  1739. data = kzalloc(count + 1, GFP_KERNEL);
  1740. if (data == NULL)
  1741. return -ENOMEM;
  1742. if (copy_from_user(data, buf, count) != 0) {
  1743. kfree(data);
  1744. return -EFAULT;
  1745. }
  1746. data_parse = data;
  1747. while ((tok = strsep(&data_parse, " ")) != NULL) {
  1748. if (!*tok)
  1749. continue;
  1750. skp = smk_import_entry(tok, 0);
  1751. if (IS_ERR(skp)) {
  1752. rc = PTR_ERR(skp);
  1753. break;
  1754. }
  1755. sop = kzalloc(sizeof(*sop), GFP_KERNEL);
  1756. if (sop == NULL) {
  1757. rc = -ENOMEM;
  1758. break;
  1759. }
  1760. sop->smk_label = skp;
  1761. list_add_rcu(&sop->list, &list_tmp);
  1762. }
  1763. kfree(data);
  1764. /*
  1765. * Clear the smack_onlycap on invalid label errors. This means
  1766. * that we can pass a null string to unset the onlycap value.
  1767. *
  1768. * Importing will also reject a label beginning with '-',
  1769. * so "-usecapabilities" will also work.
  1770. *
  1771. * But do so only on invalid label, not on system errors.
  1772. * The invalid label must be first to count as clearing attempt.
  1773. */
  1774. if (rc == -EINVAL && list_empty(&list_tmp))
  1775. rc = count;
  1776. if (rc >= 0) {
  1777. mutex_lock(&smack_onlycap_lock);
  1778. smk_list_swap_rcu(&smack_onlycap_list, &list_tmp);
  1779. mutex_unlock(&smack_onlycap_lock);
  1780. }
  1781. list_for_each_entry_safe(sop, sop2, &list_tmp, list)
  1782. kfree(sop);
  1783. return rc;
  1784. }
  1785. static const struct file_operations smk_onlycap_ops = {
  1786. .open = smk_open_onlycap,
  1787. .read = seq_read,
  1788. .write = smk_write_onlycap,
  1789. .llseek = seq_lseek,
  1790. .release = seq_release,
  1791. };
  1792. #ifdef CONFIG_SECURITY_SMACK_BRINGUP
  1793. /**
  1794. * smk_read_unconfined - read() for smackfs/unconfined
  1795. * @filp: file pointer, not actually used
  1796. * @buf: where to put the result
  1797. * @cn: maximum to send along
  1798. * @ppos: where to start
  1799. *
  1800. * Returns number of bytes read or error code, as appropriate
  1801. */
  1802. static ssize_t smk_read_unconfined(struct file *filp, char __user *buf,
  1803. size_t cn, loff_t *ppos)
  1804. {
  1805. char *smack = "";
  1806. ssize_t rc = -EINVAL;
  1807. int asize;
  1808. if (*ppos != 0)
  1809. return 0;
  1810. if (smack_unconfined != NULL)
  1811. smack = smack_unconfined->smk_known;
  1812. asize = strlen(smack) + 1;
  1813. if (cn >= asize)
  1814. rc = simple_read_from_buffer(buf, cn, ppos, smack, asize);
  1815. return rc;
  1816. }
  1817. /**
  1818. * smk_write_unconfined - write() for smackfs/unconfined
  1819. * @file: file pointer, not actually used
  1820. * @buf: where to get the data from
  1821. * @count: bytes sent
  1822. * @ppos: where to start
  1823. *
  1824. * Returns number of bytes written or error code, as appropriate
  1825. */
  1826. static ssize_t smk_write_unconfined(struct file *file, const char __user *buf,
  1827. size_t count, loff_t *ppos)
  1828. {
  1829. char *data;
  1830. struct smack_known *skp;
  1831. int rc = count;
  1832. if (!smack_privileged(CAP_MAC_ADMIN))
  1833. return -EPERM;
  1834. data = kzalloc(count + 1, GFP_KERNEL);
  1835. if (data == NULL)
  1836. return -ENOMEM;
  1837. if (copy_from_user(data, buf, count) != 0) {
  1838. rc = -EFAULT;
  1839. goto freeout;
  1840. }
  1841. /*
  1842. * Clear the smack_unconfined on invalid label errors. This means
  1843. * that we can pass a null string to unset the unconfined value.
  1844. *
  1845. * Importing will also reject a label beginning with '-',
  1846. * so "-confine" will also work.
  1847. *
  1848. * But do so only on invalid label, not on system errors.
  1849. */
  1850. skp = smk_import_entry(data, count);
  1851. if (PTR_ERR(skp) == -EINVAL)
  1852. skp = NULL;
  1853. else if (IS_ERR(skp)) {
  1854. rc = PTR_ERR(skp);
  1855. goto freeout;
  1856. }
  1857. smack_unconfined = skp;
  1858. freeout:
  1859. kfree(data);
  1860. return rc;
  1861. }
  1862. static const struct file_operations smk_unconfined_ops = {
  1863. .read = smk_read_unconfined,
  1864. .write = smk_write_unconfined,
  1865. .llseek = default_llseek,
  1866. };
  1867. #endif /* CONFIG_SECURITY_SMACK_BRINGUP */
  1868. /**
  1869. * smk_read_logging - read() for /smack/logging
  1870. * @filp: file pointer, not actually used
  1871. * @buf: where to put the result
  1872. * @cn: maximum to send along
  1873. * @ppos: where to start
  1874. *
  1875. * Returns number of bytes read or error code, as appropriate
  1876. */
  1877. static ssize_t smk_read_logging(struct file *filp, char __user *buf,
  1878. size_t count, loff_t *ppos)
  1879. {
  1880. char temp[32];
  1881. ssize_t rc;
  1882. if (*ppos != 0)
  1883. return 0;
  1884. sprintf(temp, "%d\n", log_policy);
  1885. rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
  1886. return rc;
  1887. }
  1888. /**
  1889. * smk_write_logging - write() for /smack/logging
  1890. * @file: file pointer, not actually used
  1891. * @buf: where to get the data from
  1892. * @count: bytes sent
  1893. * @ppos: where to start
  1894. *
  1895. * Returns number of bytes written or error code, as appropriate
  1896. */
  1897. static ssize_t smk_write_logging(struct file *file, const char __user *buf,
  1898. size_t count, loff_t *ppos)
  1899. {
  1900. char temp[32];
  1901. int i;
  1902. if (!smack_privileged(CAP_MAC_ADMIN))
  1903. return -EPERM;
  1904. if (count >= sizeof(temp) || count == 0)
  1905. return -EINVAL;
  1906. if (copy_from_user(temp, buf, count) != 0)
  1907. return -EFAULT;
  1908. temp[count] = '\0';
  1909. if (sscanf(temp, "%d", &i) != 1)
  1910. return -EINVAL;
  1911. if (i < 0 || i > 3)
  1912. return -EINVAL;
  1913. log_policy = i;
  1914. return count;
  1915. }
  1916. static const struct file_operations smk_logging_ops = {
  1917. .read = smk_read_logging,
  1918. .write = smk_write_logging,
  1919. .llseek = default_llseek,
  1920. };
  1921. /*
  1922. * Seq_file read operations for /smack/load-self
  1923. */
  1924. static void *load_self_seq_start(struct seq_file *s, loff_t *pos)
  1925. {
  1926. struct task_smack *tsp = current_security();
  1927. return smk_seq_start(s, pos, &tsp->smk_rules);
  1928. }
  1929. static void *load_self_seq_next(struct seq_file *s, void *v, loff_t *pos)
  1930. {
  1931. struct task_smack *tsp = current_security();
  1932. return smk_seq_next(s, v, pos, &tsp->smk_rules);
  1933. }
  1934. static int load_self_seq_show(struct seq_file *s, void *v)
  1935. {
  1936. struct list_head *list = v;
  1937. struct smack_rule *srp =
  1938. list_entry_rcu(list, struct smack_rule, list);
  1939. smk_rule_show(s, srp, SMK_LABELLEN);
  1940. return 0;
  1941. }
  1942. static const struct seq_operations load_self_seq_ops = {
  1943. .start = load_self_seq_start,
  1944. .next = load_self_seq_next,
  1945. .show = load_self_seq_show,
  1946. .stop = smk_seq_stop,
  1947. };
  1948. /**
  1949. * smk_open_load_self - open() for /smack/load-self2
  1950. * @inode: inode structure representing file
  1951. * @file: "load" file pointer
  1952. *
  1953. * For reading, use load_seq_* seq_file reading operations.
  1954. */
  1955. static int smk_open_load_self(struct inode *inode, struct file *file)
  1956. {
  1957. return seq_open(file, &load_self_seq_ops);
  1958. }
  1959. /**
  1960. * smk_write_load_self - write() for /smack/load-self
  1961. * @file: file pointer, not actually used
  1962. * @buf: where to get the data from
  1963. * @count: bytes sent
  1964. * @ppos: where to start - must be 0
  1965. *
  1966. */
  1967. static ssize_t smk_write_load_self(struct file *file, const char __user *buf,
  1968. size_t count, loff_t *ppos)
  1969. {
  1970. struct task_smack *tsp = current_security();
  1971. return smk_write_rules_list(file, buf, count, ppos, &tsp->smk_rules,
  1972. &tsp->smk_rules_lock, SMK_FIXED24_FMT);
  1973. }
  1974. static const struct file_operations smk_load_self_ops = {
  1975. .open = smk_open_load_self,
  1976. .read = seq_read,
  1977. .llseek = seq_lseek,
  1978. .write = smk_write_load_self,
  1979. .release = seq_release,
  1980. };
  1981. /**
  1982. * smk_user_access - handle access check transaction
  1983. * @file: file pointer
  1984. * @buf: data from user space
  1985. * @count: bytes sent
  1986. * @ppos: where to start - must be 0
  1987. */
  1988. static ssize_t smk_user_access(struct file *file, const char __user *buf,
  1989. size_t count, loff_t *ppos, int format)
  1990. {
  1991. struct smack_parsed_rule rule;
  1992. char *data;
  1993. int res;
  1994. data = simple_transaction_get(file, buf, count);
  1995. if (IS_ERR(data))
  1996. return PTR_ERR(data);
  1997. if (format == SMK_FIXED24_FMT) {
  1998. if (count < SMK_LOADLEN)
  1999. return -EINVAL;
  2000. res = smk_parse_rule(data, &rule, 0);
  2001. } else {
  2002. /*
  2003. * simple_transaction_get() returns null-terminated data
  2004. */
  2005. res = smk_parse_long_rule(data, &rule, 0, 3);
  2006. }
  2007. if (res >= 0)
  2008. res = smk_access(rule.smk_subject, rule.smk_object,
  2009. rule.smk_access1, NULL);
  2010. else if (res != -ENOENT)
  2011. return res;
  2012. /*
  2013. * smk_access() can return a value > 0 in the "bringup" case.
  2014. */
  2015. data[0] = res >= 0 ? '1' : '0';
  2016. data[1] = '\0';
  2017. simple_transaction_set(file, 2);
  2018. if (format == SMK_FIXED24_FMT)
  2019. return SMK_LOADLEN;
  2020. return count;
  2021. }
  2022. /**
  2023. * smk_write_access - handle access check transaction
  2024. * @file: file pointer
  2025. * @buf: data from user space
  2026. * @count: bytes sent
  2027. * @ppos: where to start - must be 0
  2028. */
  2029. static ssize_t smk_write_access(struct file *file, const char __user *buf,
  2030. size_t count, loff_t *ppos)
  2031. {
  2032. return smk_user_access(file, buf, count, ppos, SMK_FIXED24_FMT);
  2033. }
  2034. static const struct file_operations smk_access_ops = {
  2035. .write = smk_write_access,
  2036. .read = simple_transaction_read,
  2037. .release = simple_transaction_release,
  2038. .llseek = generic_file_llseek,
  2039. };
  2040. /*
  2041. * Seq_file read operations for /smack/load2
  2042. */
  2043. static int load2_seq_show(struct seq_file *s, void *v)
  2044. {
  2045. struct list_head *list = v;
  2046. struct smack_master_list *smlp =
  2047. list_entry_rcu(list, struct smack_master_list, list);
  2048. smk_rule_show(s, smlp->smk_rule, SMK_LONGLABEL);
  2049. return 0;
  2050. }
  2051. static const struct seq_operations load2_seq_ops = {
  2052. .start = load2_seq_start,
  2053. .next = load2_seq_next,
  2054. .show = load2_seq_show,
  2055. .stop = smk_seq_stop,
  2056. };
  2057. /**
  2058. * smk_open_load2 - open() for /smack/load2
  2059. * @inode: inode structure representing file
  2060. * @file: "load2" file pointer
  2061. *
  2062. * For reading, use load2_seq_* seq_file reading operations.
  2063. */
  2064. static int smk_open_load2(struct inode *inode, struct file *file)
  2065. {
  2066. return seq_open(file, &load2_seq_ops);
  2067. }
  2068. /**
  2069. * smk_write_load2 - write() for /smack/load2
  2070. * @file: file pointer, not actually used
  2071. * @buf: where to get the data from
  2072. * @count: bytes sent
  2073. * @ppos: where to start - must be 0
  2074. *
  2075. */
  2076. static ssize_t smk_write_load2(struct file *file, const char __user *buf,
  2077. size_t count, loff_t *ppos)
  2078. {
  2079. /*
  2080. * Must have privilege.
  2081. */
  2082. if (!smack_privileged(CAP_MAC_ADMIN))
  2083. return -EPERM;
  2084. return smk_write_rules_list(file, buf, count, ppos, NULL, NULL,
  2085. SMK_LONG_FMT);
  2086. }
  2087. static const struct file_operations smk_load2_ops = {
  2088. .open = smk_open_load2,
  2089. .read = seq_read,
  2090. .llseek = seq_lseek,
  2091. .write = smk_write_load2,
  2092. .release = seq_release,
  2093. };
  2094. /*
  2095. * Seq_file read operations for /smack/load-self2
  2096. */
  2097. static void *load_self2_seq_start(struct seq_file *s, loff_t *pos)
  2098. {
  2099. struct task_smack *tsp = current_security();
  2100. return smk_seq_start(s, pos, &tsp->smk_rules);
  2101. }
  2102. static void *load_self2_seq_next(struct seq_file *s, void *v, loff_t *pos)
  2103. {
  2104. struct task_smack *tsp = current_security();
  2105. return smk_seq_next(s, v, pos, &tsp->smk_rules);
  2106. }
  2107. static int load_self2_seq_show(struct seq_file *s, void *v)
  2108. {
  2109. struct list_head *list = v;
  2110. struct smack_rule *srp =
  2111. list_entry_rcu(list, struct smack_rule, list);
  2112. smk_rule_show(s, srp, SMK_LONGLABEL);
  2113. return 0;
  2114. }
  2115. static const struct seq_operations load_self2_seq_ops = {
  2116. .start = load_self2_seq_start,
  2117. .next = load_self2_seq_next,
  2118. .show = load_self2_seq_show,
  2119. .stop = smk_seq_stop,
  2120. };
  2121. /**
  2122. * smk_open_load_self2 - open() for /smack/load-self2
  2123. * @inode: inode structure representing file
  2124. * @file: "load" file pointer
  2125. *
  2126. * For reading, use load_seq_* seq_file reading operations.
  2127. */
  2128. static int smk_open_load_self2(struct inode *inode, struct file *file)
  2129. {
  2130. return seq_open(file, &load_self2_seq_ops);
  2131. }
  2132. /**
  2133. * smk_write_load_self2 - write() for /smack/load-self2
  2134. * @file: file pointer, not actually used
  2135. * @buf: where to get the data from
  2136. * @count: bytes sent
  2137. * @ppos: where to start - must be 0
  2138. *
  2139. */
  2140. static ssize_t smk_write_load_self2(struct file *file, const char __user *buf,
  2141. size_t count, loff_t *ppos)
  2142. {
  2143. struct task_smack *tsp = current_security();
  2144. return smk_write_rules_list(file, buf, count, ppos, &tsp->smk_rules,
  2145. &tsp->smk_rules_lock, SMK_LONG_FMT);
  2146. }
  2147. static const struct file_operations smk_load_self2_ops = {
  2148. .open = smk_open_load_self2,
  2149. .read = seq_read,
  2150. .llseek = seq_lseek,
  2151. .write = smk_write_load_self2,
  2152. .release = seq_release,
  2153. };
  2154. /**
  2155. * smk_write_access2 - handle access check transaction
  2156. * @file: file pointer
  2157. * @buf: data from user space
  2158. * @count: bytes sent
  2159. * @ppos: where to start - must be 0
  2160. */
  2161. static ssize_t smk_write_access2(struct file *file, const char __user *buf,
  2162. size_t count, loff_t *ppos)
  2163. {
  2164. return smk_user_access(file, buf, count, ppos, SMK_LONG_FMT);
  2165. }
  2166. static const struct file_operations smk_access2_ops = {
  2167. .write = smk_write_access2,
  2168. .read = simple_transaction_read,
  2169. .release = simple_transaction_release,
  2170. .llseek = generic_file_llseek,
  2171. };
  2172. /**
  2173. * smk_write_revoke_subj - write() for /smack/revoke-subject
  2174. * @file: file pointer
  2175. * @buf: data from user space
  2176. * @count: bytes sent
  2177. * @ppos: where to start - must be 0
  2178. */
  2179. static ssize_t smk_write_revoke_subj(struct file *file, const char __user *buf,
  2180. size_t count, loff_t *ppos)
  2181. {
  2182. char *data;
  2183. const char *cp;
  2184. struct smack_known *skp;
  2185. struct smack_rule *sp;
  2186. struct list_head *rule_list;
  2187. struct mutex *rule_lock;
  2188. int rc = count;
  2189. if (*ppos != 0)
  2190. return -EINVAL;
  2191. if (!smack_privileged(CAP_MAC_ADMIN))
  2192. return -EPERM;
  2193. if (count == 0 || count > SMK_LONGLABEL)
  2194. return -EINVAL;
  2195. data = kzalloc(count, GFP_KERNEL);
  2196. if (data == NULL)
  2197. return -ENOMEM;
  2198. if (copy_from_user(data, buf, count) != 0) {
  2199. rc = -EFAULT;
  2200. goto out_data;
  2201. }
  2202. cp = smk_parse_smack(data, count);
  2203. if (IS_ERR(cp)) {
  2204. rc = PTR_ERR(cp);
  2205. goto out_data;
  2206. }
  2207. skp = smk_find_entry(cp);
  2208. if (skp == NULL)
  2209. goto out_cp;
  2210. rule_list = &skp->smk_rules;
  2211. rule_lock = &skp->smk_rules_lock;
  2212. mutex_lock(rule_lock);
  2213. list_for_each_entry_rcu(sp, rule_list, list)
  2214. sp->smk_access = 0;
  2215. mutex_unlock(rule_lock);
  2216. out_cp:
  2217. kfree(cp);
  2218. out_data:
  2219. kfree(data);
  2220. return rc;
  2221. }
  2222. static const struct file_operations smk_revoke_subj_ops = {
  2223. .write = smk_write_revoke_subj,
  2224. .read = simple_transaction_read,
  2225. .release = simple_transaction_release,
  2226. .llseek = generic_file_llseek,
  2227. };
  2228. /**
  2229. * smk_init_sysfs - initialize /sys/fs/smackfs
  2230. *
  2231. */
  2232. static int smk_init_sysfs(void)
  2233. {
  2234. return sysfs_create_mount_point(fs_kobj, "smackfs");
  2235. }
  2236. /**
  2237. * smk_write_change_rule - write() for /smack/change-rule
  2238. * @file: file pointer
  2239. * @buf: data from user space
  2240. * @count: bytes sent
  2241. * @ppos: where to start - must be 0
  2242. */
  2243. static ssize_t smk_write_change_rule(struct file *file, const char __user *buf,
  2244. size_t count, loff_t *ppos)
  2245. {
  2246. /*
  2247. * Must have privilege.
  2248. */
  2249. if (!smack_privileged(CAP_MAC_ADMIN))
  2250. return -EPERM;
  2251. return smk_write_rules_list(file, buf, count, ppos, NULL, NULL,
  2252. SMK_CHANGE_FMT);
  2253. }
  2254. static const struct file_operations smk_change_rule_ops = {
  2255. .write = smk_write_change_rule,
  2256. .read = simple_transaction_read,
  2257. .release = simple_transaction_release,
  2258. .llseek = generic_file_llseek,
  2259. };
  2260. /**
  2261. * smk_read_syslog - read() for smackfs/syslog
  2262. * @filp: file pointer, not actually used
  2263. * @buf: where to put the result
  2264. * @cn: maximum to send along
  2265. * @ppos: where to start
  2266. *
  2267. * Returns number of bytes read or error code, as appropriate
  2268. */
  2269. static ssize_t smk_read_syslog(struct file *filp, char __user *buf,
  2270. size_t cn, loff_t *ppos)
  2271. {
  2272. struct smack_known *skp;
  2273. ssize_t rc = -EINVAL;
  2274. int asize;
  2275. if (*ppos != 0)
  2276. return 0;
  2277. if (smack_syslog_label == NULL)
  2278. skp = &smack_known_star;
  2279. else
  2280. skp = smack_syslog_label;
  2281. asize = strlen(skp->smk_known) + 1;
  2282. if (cn >= asize)
  2283. rc = simple_read_from_buffer(buf, cn, ppos, skp->smk_known,
  2284. asize);
  2285. return rc;
  2286. }
  2287. /**
  2288. * smk_write_syslog - write() for smackfs/syslog
  2289. * @file: file pointer, not actually used
  2290. * @buf: where to get the data from
  2291. * @count: bytes sent
  2292. * @ppos: where to start
  2293. *
  2294. * Returns number of bytes written or error code, as appropriate
  2295. */
  2296. static ssize_t smk_write_syslog(struct file *file, const char __user *buf,
  2297. size_t count, loff_t *ppos)
  2298. {
  2299. char *data;
  2300. struct smack_known *skp;
  2301. int rc = count;
  2302. if (!smack_privileged(CAP_MAC_ADMIN))
  2303. return -EPERM;
  2304. data = kzalloc(count + 1, GFP_KERNEL);
  2305. if (data == NULL)
  2306. return -ENOMEM;
  2307. if (copy_from_user(data, buf, count) != 0)
  2308. rc = -EFAULT;
  2309. else {
  2310. skp = smk_import_entry(data, count);
  2311. if (IS_ERR(skp))
  2312. rc = PTR_ERR(skp);
  2313. else
  2314. smack_syslog_label = skp;
  2315. }
  2316. kfree(data);
  2317. return rc;
  2318. }
  2319. static const struct file_operations smk_syslog_ops = {
  2320. .read = smk_read_syslog,
  2321. .write = smk_write_syslog,
  2322. .llseek = default_llseek,
  2323. };
  2324. /**
  2325. * smk_read_ptrace - read() for /smack/ptrace
  2326. * @filp: file pointer, not actually used
  2327. * @buf: where to put the result
  2328. * @count: maximum to send along
  2329. * @ppos: where to start
  2330. *
  2331. * Returns number of bytes read or error code, as appropriate
  2332. */
  2333. static ssize_t smk_read_ptrace(struct file *filp, char __user *buf,
  2334. size_t count, loff_t *ppos)
  2335. {
  2336. char temp[32];
  2337. ssize_t rc;
  2338. if (*ppos != 0)
  2339. return 0;
  2340. sprintf(temp, "%d\n", smack_ptrace_rule);
  2341. rc = simple_read_from_buffer(buf, count, ppos, temp, strlen(temp));
  2342. return rc;
  2343. }
  2344. /**
  2345. * smk_write_ptrace - write() for /smack/ptrace
  2346. * @file: file pointer
  2347. * @buf: data from user space
  2348. * @count: bytes sent
  2349. * @ppos: where to start - must be 0
  2350. */
  2351. static ssize_t smk_write_ptrace(struct file *file, const char __user *buf,
  2352. size_t count, loff_t *ppos)
  2353. {
  2354. char temp[32];
  2355. int i;
  2356. if (!smack_privileged(CAP_MAC_ADMIN))
  2357. return -EPERM;
  2358. if (*ppos != 0 || count >= sizeof(temp) || count == 0)
  2359. return -EINVAL;
  2360. if (copy_from_user(temp, buf, count) != 0)
  2361. return -EFAULT;
  2362. temp[count] = '\0';
  2363. if (sscanf(temp, "%d", &i) != 1)
  2364. return -EINVAL;
  2365. if (i < SMACK_PTRACE_DEFAULT || i > SMACK_PTRACE_MAX)
  2366. return -EINVAL;
  2367. smack_ptrace_rule = i;
  2368. return count;
  2369. }
  2370. static const struct file_operations smk_ptrace_ops = {
  2371. .write = smk_write_ptrace,
  2372. .read = smk_read_ptrace,
  2373. .llseek = default_llseek,
  2374. };
  2375. /**
  2376. * smk_fill_super - fill the smackfs superblock
  2377. * @sb: the empty superblock
  2378. * @data: unused
  2379. * @silent: unused
  2380. *
  2381. * Fill in the well known entries for the smack filesystem
  2382. *
  2383. * Returns 0 on success, an error code on failure
  2384. */
  2385. static int smk_fill_super(struct super_block *sb, void *data, int silent)
  2386. {
  2387. int rc;
  2388. struct inode *root_inode;
  2389. static struct tree_descr smack_files[] = {
  2390. [SMK_LOAD] = {
  2391. "load", &smk_load_ops, S_IRUGO|S_IWUSR},
  2392. [SMK_CIPSO] = {
  2393. "cipso", &smk_cipso_ops, S_IRUGO|S_IWUSR},
  2394. [SMK_DOI] = {
  2395. "doi", &smk_doi_ops, S_IRUGO|S_IWUSR},
  2396. [SMK_DIRECT] = {
  2397. "direct", &smk_direct_ops, S_IRUGO|S_IWUSR},
  2398. [SMK_AMBIENT] = {
  2399. "ambient", &smk_ambient_ops, S_IRUGO|S_IWUSR},
  2400. [SMK_NET4ADDR] = {
  2401. "netlabel", &smk_net4addr_ops, S_IRUGO|S_IWUSR},
  2402. [SMK_ONLYCAP] = {
  2403. "onlycap", &smk_onlycap_ops, S_IRUGO|S_IWUSR},
  2404. [SMK_LOGGING] = {
  2405. "logging", &smk_logging_ops, S_IRUGO|S_IWUSR},
  2406. [SMK_LOAD_SELF] = {
  2407. "load-self", &smk_load_self_ops, S_IRUGO|S_IWUGO},
  2408. [SMK_ACCESSES] = {
  2409. "access", &smk_access_ops, S_IRUGO|S_IWUGO},
  2410. [SMK_MAPPED] = {
  2411. "mapped", &smk_mapped_ops, S_IRUGO|S_IWUSR},
  2412. [SMK_LOAD2] = {
  2413. "load2", &smk_load2_ops, S_IRUGO|S_IWUSR},
  2414. [SMK_LOAD_SELF2] = {
  2415. "load-self2", &smk_load_self2_ops, S_IRUGO|S_IWUGO},
  2416. [SMK_ACCESS2] = {
  2417. "access2", &smk_access2_ops, S_IRUGO|S_IWUGO},
  2418. [SMK_CIPSO2] = {
  2419. "cipso2", &smk_cipso2_ops, S_IRUGO|S_IWUSR},
  2420. [SMK_REVOKE_SUBJ] = {
  2421. "revoke-subject", &smk_revoke_subj_ops,
  2422. S_IRUGO|S_IWUSR},
  2423. [SMK_CHANGE_RULE] = {
  2424. "change-rule", &smk_change_rule_ops, S_IRUGO|S_IWUSR},
  2425. [SMK_SYSLOG] = {
  2426. "syslog", &smk_syslog_ops, S_IRUGO|S_IWUSR},
  2427. [SMK_PTRACE] = {
  2428. "ptrace", &smk_ptrace_ops, S_IRUGO|S_IWUSR},
  2429. #ifdef CONFIG_SECURITY_SMACK_BRINGUP
  2430. [SMK_UNCONFINED] = {
  2431. "unconfined", &smk_unconfined_ops, S_IRUGO|S_IWUSR},
  2432. #endif
  2433. #if IS_ENABLED(CONFIG_IPV6)
  2434. [SMK_NET6ADDR] = {
  2435. "ipv6host", &smk_net6addr_ops, S_IRUGO|S_IWUSR},
  2436. #endif /* CONFIG_IPV6 */
  2437. /* last one */
  2438. {""}
  2439. };
  2440. rc = simple_fill_super(sb, SMACK_MAGIC, smack_files);
  2441. if (rc != 0) {
  2442. printk(KERN_ERR "%s failed %d while creating inodes\n",
  2443. __func__, rc);
  2444. return rc;
  2445. }
  2446. root_inode = d_inode(sb->s_root);
  2447. return 0;
  2448. }
  2449. /**
  2450. * smk_mount - get the smackfs superblock
  2451. * @fs_type: passed along without comment
  2452. * @flags: passed along without comment
  2453. * @dev_name: passed along without comment
  2454. * @data: passed along without comment
  2455. *
  2456. * Just passes everything along.
  2457. *
  2458. * Returns what the lower level code does.
  2459. */
  2460. static struct dentry *smk_mount(struct file_system_type *fs_type,
  2461. int flags, const char *dev_name, void *data)
  2462. {
  2463. return mount_single(fs_type, flags, data, smk_fill_super);
  2464. }
  2465. static struct file_system_type smk_fs_type = {
  2466. .name = "smackfs",
  2467. .mount = smk_mount,
  2468. .kill_sb = kill_litter_super,
  2469. };
  2470. static struct vfsmount *smackfs_mount;
  2471. static int __init smk_preset_netlabel(struct smack_known *skp)
  2472. {
  2473. skp->smk_netlabel.domain = skp->smk_known;
  2474. skp->smk_netlabel.flags =
  2475. NETLBL_SECATTR_DOMAIN | NETLBL_SECATTR_MLS_LVL;
  2476. return smk_netlbl_mls(smack_cipso_direct, skp->smk_known,
  2477. &skp->smk_netlabel, strlen(skp->smk_known));
  2478. }
  2479. /**
  2480. * init_smk_fs - get the smackfs superblock
  2481. *
  2482. * register the smackfs
  2483. *
  2484. * Do not register smackfs if Smack wasn't enabled
  2485. * on boot. We can not put this method normally under the
  2486. * smack_init() code path since the security subsystem get
  2487. * initialized before the vfs caches.
  2488. *
  2489. * Returns true if we were not chosen on boot or if
  2490. * we were chosen and filesystem registration succeeded.
  2491. */
  2492. static int __init init_smk_fs(void)
  2493. {
  2494. int err;
  2495. int rc;
  2496. if (!security_module_enable("smack"))
  2497. return 0;
  2498. err = smk_init_sysfs();
  2499. if (err)
  2500. printk(KERN_ERR "smackfs: sysfs mountpoint problem.\n");
  2501. err = register_filesystem(&smk_fs_type);
  2502. if (!err) {
  2503. smackfs_mount = kern_mount(&smk_fs_type);
  2504. if (IS_ERR(smackfs_mount)) {
  2505. printk(KERN_ERR "smackfs: could not mount!\n");
  2506. err = PTR_ERR(smackfs_mount);
  2507. smackfs_mount = NULL;
  2508. }
  2509. }
  2510. smk_cipso_doi();
  2511. smk_unlbl_ambient(NULL);
  2512. rc = smk_preset_netlabel(&smack_known_floor);
  2513. if (err == 0 && rc < 0)
  2514. err = rc;
  2515. rc = smk_preset_netlabel(&smack_known_hat);
  2516. if (err == 0 && rc < 0)
  2517. err = rc;
  2518. rc = smk_preset_netlabel(&smack_known_huh);
  2519. if (err == 0 && rc < 0)
  2520. err = rc;
  2521. rc = smk_preset_netlabel(&smack_known_invalid);
  2522. if (err == 0 && rc < 0)
  2523. err = rc;
  2524. rc = smk_preset_netlabel(&smack_known_star);
  2525. if (err == 0 && rc < 0)
  2526. err = rc;
  2527. rc = smk_preset_netlabel(&smack_known_web);
  2528. if (err == 0 && rc < 0)
  2529. err = rc;
  2530. return err;
  2531. }
  2532. __initcall(init_smk_fs);