@@ -306,57 +306,138 @@ static int devcgroup_seq_show(struct seq_file *m, void *v)
306306}
307307
308308/**
309- * may_access - verifies if a new exception is part of what is allowed
310- * by a dev cgroup based on the default policy +
311- * exceptions. This is used to make sure a child cgroup
312- * won't have more privileges than its parent or to
313- * verify if a certain access is allowed.
314- * @dev_cgroup: dev cgroup to be tested against
315- * @refex: new exception
316- * @behavior: behavior of the exception
309+ * match_exception - iterates the exception list trying to find a complete match
310+ * @exceptions: list of exceptions
311+ * @type: device type (DEV_BLOCK or DEV_CHAR)
312+ * @major: device file major number, ~0 to match all
313+ * @minor: device file minor number, ~0 to match all
314+ * @access: permission mask (ACC_READ, ACC_WRITE, ACC_MKNOD)
315+ *
316+ * It is considered a complete match if an exception is found that will
317+ * contain the entire range of provided parameters.
318+ *
319+ * Return: true in case it matches an exception completely
317320 */
318- static bool may_access (struct dev_cgroup * dev_cgroup ,
319- struct dev_exception_item * refex ,
320- enum devcg_behavior behavior )
321+ static bool match_exception (struct list_head * exceptions , short type ,
322+ u32 major , u32 minor , short access )
321323{
322324 struct dev_exception_item * ex ;
323- bool match = false;
324325
325- rcu_lockdep_assert (rcu_read_lock_held () ||
326- lockdep_is_held (& devcgroup_mutex ),
327- "device_cgroup::may_access() called without proper synchronization" );
326+ list_for_each_entry_rcu (ex , exceptions , list ) {
327+ if ((type & DEV_BLOCK ) && !(ex -> type & DEV_BLOCK ))
328+ continue ;
329+ if ((type & DEV_CHAR ) && !(ex -> type & DEV_CHAR ))
330+ continue ;
331+ if (ex -> major != ~0 && ex -> major != major )
332+ continue ;
333+ if (ex -> minor != ~0 && ex -> minor != minor )
334+ continue ;
335+ /* provided access cannot have more than the exception rule */
336+ if (access & (~ex -> access ))
337+ continue ;
338+ return true;
339+ }
340+ return false;
341+ }
342+
343+ /**
344+ * match_exception_partial - iterates the exception list trying to find a partial match
345+ * @exceptions: list of exceptions
346+ * @type: device type (DEV_BLOCK or DEV_CHAR)
347+ * @major: device file major number, ~0 to match all
348+ * @minor: device file minor number, ~0 to match all
349+ * @access: permission mask (ACC_READ, ACC_WRITE, ACC_MKNOD)
350+ *
351+ * It is considered a partial match if an exception's range is found to
352+ * contain *any* of the devices specified by provided parameters. This is
353+ * used to make sure no extra access is being granted that is forbidden by
354+ * any of the exception list.
355+ *
356+ * Return: true in case the provided range mat matches an exception completely
357+ */
358+ static bool match_exception_partial (struct list_head * exceptions , short type ,
359+ u32 major , u32 minor , short access )
360+ {
361+ struct dev_exception_item * ex ;
328362
329- list_for_each_entry_rcu (ex , & dev_cgroup -> exceptions , list ) {
330- if ((refex -> type & DEV_BLOCK ) && !(ex -> type & DEV_BLOCK ))
363+ list_for_each_entry_rcu (ex , exceptions , list ) {
364+ if ((type & DEV_BLOCK ) && !(ex -> type & DEV_BLOCK ))
331365 continue ;
332- if ((refex -> type & DEV_CHAR ) && !(ex -> type & DEV_CHAR ))
366+ if ((type & DEV_CHAR ) && !(ex -> type & DEV_CHAR ))
333367 continue ;
334- if (ex -> major != ~0 && ex -> major != refex -> major )
368+ /*
369+ * We must be sure that both the exception and the provided
370+ * range aren't masking all devices
371+ */
372+ if (ex -> major != ~0 && major != ~0 && ex -> major != major )
335373 continue ;
336- if (ex -> minor != ~0 && ex -> minor != refex -> minor )
374+ if (ex -> minor != ~0 && minor != ~ 0 && ex -> minor != minor )
337375 continue ;
338- if (refex -> access & (~ex -> access ))
376+ /*
377+ * In order to make sure the provided range isn't matching
378+ * an exception, all its access bits shouldn't match the
379+ * exception's access bits
380+ */
381+ if (!(access & ex -> access ))
339382 continue ;
340- match = true;
341- break ;
383+ return true;
342384 }
385+ return false;
386+ }
387+
388+ /**
389+ * verify_new_ex - verifies if a new exception is allowed by parent cgroup's permissions
390+ * @dev_cgroup: dev cgroup to be tested against
391+ * @refex: new exception
392+ * @behavior: behavior of the exception's dev_cgroup
393+ *
394+ * This is used to make sure a child cgroup won't have more privileges
395+ * than its parent
396+ */
397+ static bool verify_new_ex (struct dev_cgroup * dev_cgroup ,
398+ struct dev_exception_item * refex ,
399+ enum devcg_behavior behavior )
400+ {
401+ bool match = false;
402+
403+ rcu_lockdep_assert (rcu_read_lock_held () ||
404+ lockdep_is_held (& devcgroup_mutex ),
405+ "device_cgroup:verify_new_ex called without proper synchronization" );
343406
344407 if (dev_cgroup -> behavior == DEVCG_DEFAULT_ALLOW ) {
345408 if (behavior == DEVCG_DEFAULT_ALLOW ) {
346- /* the exception will deny access to certain devices */
409+ /*
410+ * new exception in the child doesn't matter, only
411+ * adding extra restrictions
412+ */
347413 return true;
348414 } else {
349- /* the exception will allow access to certain devices */
415+ /*
416+ * new exception in the child will add more devices
417+ * that can be acessed, so it can't match any of
418+ * parent's exceptions, even slightly
419+ */
420+ match = match_exception_partial (& dev_cgroup -> exceptions ,
421+ refex -> type ,
422+ refex -> major ,
423+ refex -> minor ,
424+ refex -> access );
425+
350426 if (match )
351- /*
352- * a new exception allowing access shouldn't
353- * match an parent's exception
354- */
355427 return false;
356428 return true;
357429 }
358430 } else {
359- /* only behavior == DEVCG_DEFAULT_DENY allowed here */
431+ /*
432+ * Only behavior == DEVCG_DEFAULT_DENY allowed here, therefore
433+ * the new exception will add access to more devices and must
434+ * be contained completely in an parent's exception to be
435+ * allowed
436+ */
437+ match = match_exception (& dev_cgroup -> exceptions , refex -> type ,
438+ refex -> major , refex -> minor ,
439+ refex -> access );
440+
360441 if (match )
361442 /* parent has an exception that matches the proposed */
362443 return true;
@@ -378,7 +459,38 @@ static int parent_has_perm(struct dev_cgroup *childcg,
378459
379460 if (!parent )
380461 return 1 ;
381- return may_access (parent , ex , childcg -> behavior );
462+ return verify_new_ex (parent , ex , childcg -> behavior );
463+ }
464+
465+ /**
466+ * parent_allows_removal - verify if it's ok to remove an exception
467+ * @childcg: child cgroup from where the exception will be removed
468+ * @ex: exception being removed
469+ *
470+ * When removing an exception in cgroups with default ALLOW policy, it must
471+ * be checked if removing it will give the child cgroup more access than the
472+ * parent.
473+ *
474+ * Return: true if it's ok to remove exception, false otherwise
475+ */
476+ static bool parent_allows_removal (struct dev_cgroup * childcg ,
477+ struct dev_exception_item * ex )
478+ {
479+ struct dev_cgroup * parent = css_to_devcgroup (css_parent (& childcg -> css ));
480+
481+ if (!parent )
482+ return true;
483+
484+ /* It's always allowed to remove access to devices */
485+ if (childcg -> behavior == DEVCG_DEFAULT_DENY )
486+ return true;
487+
488+ /*
489+ * Make sure you're not removing part or a whole exception existing in
490+ * the parent cgroup
491+ */
492+ return !match_exception_partial (& parent -> exceptions , ex -> type ,
493+ ex -> major , ex -> minor , ex -> access );
382494}
383495
384496/**
@@ -616,17 +728,21 @@ static int devcgroup_update_access(struct dev_cgroup *devcgroup,
616728
617729 switch (filetype ) {
618730 case DEVCG_ALLOW :
619- if (!parent_has_perm (devcgroup , & ex ))
620- return - EPERM ;
621731 /*
622732 * If the default policy is to allow by default, try to remove
623733 * an matching exception instead. And be silent about it: we
624734 * don't want to break compatibility
625735 */
626736 if (devcgroup -> behavior == DEVCG_DEFAULT_ALLOW ) {
737+ /* Check if the parent allows removing it first */
738+ if (!parent_allows_removal (devcgroup , & ex ))
739+ return - EPERM ;
627740 dev_exception_rm (devcgroup , & ex );
628- return 0 ;
741+ break ;
629742 }
743+
744+ if (!parent_has_perm (devcgroup , & ex ))
745+ return - EPERM ;
630746 rc = dev_exception_add (devcgroup , & ex );
631747 break ;
632748 case DEVCG_DENY :
@@ -704,18 +820,18 @@ static int __devcgroup_check_permission(short type, u32 major, u32 minor,
704820 short access )
705821{
706822 struct dev_cgroup * dev_cgroup ;
707- struct dev_exception_item ex ;
708- int rc ;
709-
710- memset (& ex , 0 , sizeof (ex ));
711- ex .type = type ;
712- ex .major = major ;
713- ex .minor = minor ;
714- ex .access = access ;
823+ bool rc ;
715824
716825 rcu_read_lock ();
717826 dev_cgroup = task_devcgroup (current );
718- rc = may_access (dev_cgroup , & ex , dev_cgroup -> behavior );
827+ if (dev_cgroup -> behavior == DEVCG_DEFAULT_ALLOW )
828+ /* Can't match any of the exceptions, even partially */
829+ rc = !match_exception_partial (& dev_cgroup -> exceptions ,
830+ type , major , minor , access );
831+ else
832+ /* Need to match completely one exception to be allowed */
833+ rc = match_exception (& dev_cgroup -> exceptions , type , major ,
834+ minor , access );
719835 rcu_read_unlock ();
720836
721837 if (!rc )
0 commit comments