18c2ecf20Sopenharmony_ci===========================
28c2ecf20Sopenharmony_ciDevice Whitelist Controller
38c2ecf20Sopenharmony_ci===========================
48c2ecf20Sopenharmony_ci
58c2ecf20Sopenharmony_ci1. Description
68c2ecf20Sopenharmony_ci==============
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ciImplement a cgroup to track and enforce open and mknod restrictions
98c2ecf20Sopenharmony_cion device files.  A device cgroup associates a device access
108c2ecf20Sopenharmony_ciwhitelist with each cgroup.  A whitelist entry has 4 fields.
118c2ecf20Sopenharmony_ci'type' is a (all), c (char), or b (block).  'all' means it applies
128c2ecf20Sopenharmony_cito all types and all major and minor numbers.  Major and minor are
138c2ecf20Sopenharmony_cieither an integer or * for all.  Access is a composition of r
148c2ecf20Sopenharmony_ci(read), w (write), and m (mknod).
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ciThe root device cgroup starts with rwm to 'all'.  A child device
178c2ecf20Sopenharmony_cicgroup gets a copy of the parent.  Administrators can then remove
188c2ecf20Sopenharmony_cidevices from the whitelist or add new entries.  A child cgroup can
198c2ecf20Sopenharmony_cinever receive a device access which is denied by its parent.
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci2. User Interface
228c2ecf20Sopenharmony_ci=================
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ciAn entry is added using devices.allow, and removed using
258c2ecf20Sopenharmony_cidevices.deny.  For instance::
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci	echo 'c 1:3 mr' > /sys/fs/cgroup/1/devices.allow
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ciallows cgroup 1 to read and mknod the device usually known as
308c2ecf20Sopenharmony_ci/dev/null.  Doing::
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci	echo a > /sys/fs/cgroup/1/devices.deny
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ciwill remove the default 'a *:* rwm' entry. Doing::
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci	echo a > /sys/fs/cgroup/1/devices.allow
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ciwill add the 'a *:* rwm' entry to the whitelist.
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci3. Security
418c2ecf20Sopenharmony_ci===========
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ciAny task can move itself between cgroups.  This clearly won't
448c2ecf20Sopenharmony_cisuffice, but we can decide the best way to adequately restrict
458c2ecf20Sopenharmony_cimovement as people get some experience with this.  We may just want
468c2ecf20Sopenharmony_cito require CAP_SYS_ADMIN, which at least is a separate bit from
478c2ecf20Sopenharmony_ciCAP_MKNOD.  We may want to just refuse moving to a cgroup which
488c2ecf20Sopenharmony_ciisn't a descendant of the current one.  Or we may want to use
498c2ecf20Sopenharmony_ciCAP_MAC_ADMIN, since we really are trying to lock down root.
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ciCAP_SYS_ADMIN is needed to modify the whitelist or move another
528c2ecf20Sopenharmony_citask to a new cgroup.  (Again we'll probably want to change that).
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ciA cgroup may not be granted more permissions than the cgroup's
558c2ecf20Sopenharmony_ciparent has.
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci4. Hierarchy
588c2ecf20Sopenharmony_ci============
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_cidevice cgroups maintain hierarchy by making sure a cgroup never has more
618c2ecf20Sopenharmony_ciaccess permissions than its parent.  Every time an entry is written to
628c2ecf20Sopenharmony_cia cgroup's devices.deny file, all its children will have that entry removed
638c2ecf20Sopenharmony_cifrom their whitelist and all the locally set whitelist entries will be
648c2ecf20Sopenharmony_cire-evaluated.  In case one of the locally set whitelist entries would provide
658c2ecf20Sopenharmony_cimore access than the cgroup's parent, it'll be removed from the whitelist.
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ciExample::
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci      A
708c2ecf20Sopenharmony_ci     / \
718c2ecf20Sopenharmony_ci        B
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci    group        behavior	exceptions
748c2ecf20Sopenharmony_ci    A            allow		"b 8:* rwm", "c 116:1 rw"
758c2ecf20Sopenharmony_ci    B            deny		"c 1:3 rwm", "c 116:2 rwm", "b 3:* rwm"
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ciIf a device is denied in group A::
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci	# echo "c 116:* r" > A/devices.deny
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ciit'll propagate down and after revalidating B's entries, the whitelist entry
828c2ecf20Sopenharmony_ci"c 116:2 rwm" will be removed::
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci    group        whitelist entries                        denied devices
858c2ecf20Sopenharmony_ci    A            all                                      "b 8:* rwm", "c 116:* rw"
868c2ecf20Sopenharmony_ci    B            "c 1:3 rwm", "b 3:* rwm"                 all the rest
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ciIn case parent's exceptions change and local exceptions are not allowed
898c2ecf20Sopenharmony_cianymore, they'll be deleted.
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ciNotice that new whitelist entries will not be propagated::
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci      A
948c2ecf20Sopenharmony_ci     / \
958c2ecf20Sopenharmony_ci        B
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci    group        whitelist entries                        denied devices
988c2ecf20Sopenharmony_ci    A            "c 1:3 rwm", "c 1:5 r"                   all the rest
998c2ecf20Sopenharmony_ci    B            "c 1:3 rwm", "c 1:5 r"                   all the rest
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ciwhen adding ``c *:3 rwm``::
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci	# echo "c *:3 rwm" >A/devices.allow
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_cithe result::
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci    group        whitelist entries                        denied devices
1088c2ecf20Sopenharmony_ci    A            "c *:3 rwm", "c 1:5 r"                   all the rest
1098c2ecf20Sopenharmony_ci    B            "c 1:3 rwm", "c 1:5 r"                   all the rest
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_cibut now it'll be possible to add new entries to B::
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci	# echo "c 2:3 rwm" >B/devices.allow
1148c2ecf20Sopenharmony_ci	# echo "c 50:3 r" >B/devices.allow
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_cior even::
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci	# echo "c *:3 rwm" >B/devices.allow
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ciAllowing or denying all by writing 'a' to devices.allow or devices.deny will
1218c2ecf20Sopenharmony_cinot be possible once the device cgroups has children.
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci4.1 Hierarchy (internal implementation)
1248c2ecf20Sopenharmony_ci---------------------------------------
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_cidevice cgroups is implemented internally using a behavior (ALLOW, DENY) and a
1278c2ecf20Sopenharmony_cilist of exceptions.  The internal state is controlled using the same user
1288c2ecf20Sopenharmony_ciinterface to preserve compatibility with the previous whitelist-only
1298c2ecf20Sopenharmony_ciimplementation.  Removal or addition of exceptions that will reduce the access
1308c2ecf20Sopenharmony_cito devices will be propagated down the hierarchy.
1318c2ecf20Sopenharmony_ciFor every propagated exception, the effective rules will be re-evaluated based
1328c2ecf20Sopenharmony_cion current parent's access rules.
133