smackfs.c 69 KB

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