smackfs.c 70 KB

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