<div dir="ltr">Hi (patch author here),<div><br></div><div>This is my first "real" patch, so looking forward to some feedback! I am not sure if this behavior is the "best one", or if we should require CONFIG_CGROUP_DEVICE to be set to yes. In that case we can just abandon this patch and replace the original "#if defined(CONFIG_CGROUP_DEVICE) || defined(CONFIG_CGROUP_BPF)" with a simple "#ifdef CONFIG_CGROUP_DEVICE" and update the docs and the config.</div><div><br></div><div>It is also another alternative to keep the code in this patch and move some of it into a separate file in order to avoid all the ifdefs, and to make the split between cgroup v1 and cgroup v2 code cleaner.</div><div><br></div><div>As a reference, only Fedora is currently shipping with cgroup v2 as default (afaik.) and their kernel is compiled (5.3.7-301.fc31.x86_64) with CONFIG_CGROUP_DEVICE=y and CONFIG_CGROUP_BPF=y, so this will not affect them.</div><div><br></div><div>Odin Ugedal</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">fre. 3. apr. 2020 kl. 19:55 skrev Odin Ugedal <<a href="mailto:odin@ugedal.com">odin@ugedal.com</a>>:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Original cgroup v2 eBPF code for filtering device access made it<br>
possible to compile with CONFIG_CGROUP_DEVICE=n and still use the eBPF<br>
filtering. Change <br>
commit 4b7d4d453fc4 ("device_cgroup: Export devcgroup_check_permission")<br>
reverted this, making it required to set it to y.<br>
<br>
Since the device filtering (and all the docs) for cgroup v2 is no longer<br>
a "device controller" like it was in v1, someone might compile their<br>
kernel with CONFIG_CGROUP_DEVICE=n. Then (for linux 5.5+) the eBPF<br>
filter will not be invoked, and all processes will be allowed access<br>
to all devices, no matter what the eBPF filter says.<br>
<br>
Signed-off-by: Odin Ugedal <<a href="mailto:odin@ugedal.com" target="_blank">odin@ugedal.com</a>><br>
---<br>
drivers/gpu/drm/amd/amdkfd/kfd_priv.h | 2 +-<br>
include/linux/device_cgroup.h | 14 +++++---------<br>
security/Makefile | 2 +-<br>
security/device_cgroup.c | 19 ++++++++++++++++---<br>
4 files changed, 23 insertions(+), 14 deletions(-)<br>
<br>
diff --git a/drivers/gpu/drm/amd/amdkfd/kfd_priv.h b/drivers/gpu/drm/amd/amdkfd/kfd_priv.h<br>
index 4a3049841086..c24cad3c64ed 100644<br>
--- a/drivers/gpu/drm/amd/amdkfd/kfd_priv.h<br>
+++ b/drivers/gpu/drm/amd/amdkfd/kfd_priv.h<br>
@@ -1050,7 +1050,7 @@ void kfd_dec_compute_active(struct kfd_dev *dev);<br>
/* Check with device cgroup if @kfd device is accessible */<br>
static inline int kfd_devcgroup_check_permission(struct kfd_dev *kfd)<br>
{<br>
-#if defined(CONFIG_CGROUP_DEVICE)<br>
+#if defined(CONFIG_CGROUP_DEVICE) || defined(CONFIG_CGROUP_BPF)<br>
struct drm_device *ddev = kfd->ddev;<br>
<br>
return devcgroup_check_permission(DEVCG_DEV_CHAR, ddev->driver->major,<br>
diff --git a/include/linux/device_cgroup.h b/include/linux/device_cgroup.h<br>
index fa35b52e0002..9a72214496e5 100644<br>
--- a/include/linux/device_cgroup.h<br>
+++ b/include/linux/device_cgroup.h<br>
@@ -1,6 +1,5 @@<br>
/* SPDX-License-Identifier: GPL-2.0 */<br>
#include <linux/fs.h><br>
-#include <linux/bpf-cgroup.h><br>
<br>
#define DEVCG_ACC_MKNOD 1<br>
#define DEVCG_ACC_READ 2<br>
@@ -11,16 +10,10 @@<br>
#define DEVCG_DEV_CHAR 2<br>
#define DEVCG_DEV_ALL 4 /* this represents all devices */<br>
<br>
-#ifdef CONFIG_CGROUP_DEVICE<br>
-int devcgroup_check_permission(short type, u32 major, u32 minor,<br>
- short access);<br>
-#else<br>
-static inline int devcgroup_check_permission(short type, u32 major, u32 minor,<br>
- short access)<br>
-{ return 0; }<br>
-#endif<br>
<br>
#if defined(CONFIG_CGROUP_DEVICE) || defined(CONFIG_CGROUP_BPF)<br>
+int devcgroup_check_permission(short type, u32 major, u32 minor,<br>
+ short access);<br>
static inline int devcgroup_inode_permission(struct inode *inode, int mask)<br>
{<br>
short type, access = 0;<br>
@@ -61,6 +54,9 @@ static inline int devcgroup_inode_mknod(int mode, dev_t dev)<br>
}<br>
<br>
#else<br>
+static inline int devcgroup_check_permission(short type, u32 major, u32 minor,<br>
+ short access)<br>
+{ return 0; }<br>
static inline int devcgroup_inode_permission(struct inode *inode, int mask)<br>
{ return 0; }<br>
static inline int devcgroup_inode_mknod(int mode, dev_t dev)<br>
diff --git a/security/Makefile b/security/Makefile<br>
index 22e73a3482bd..3baf435de541 100644<br>
--- a/security/Makefile<br>
+++ b/security/Makefile<br>
@@ -30,7 +30,7 @@ obj-$(CONFIG_SECURITY_YAMA) += yama/<br>
obj-$(CONFIG_SECURITY_LOADPIN) += loadpin/<br>
obj-$(CONFIG_SECURITY_SAFESETID) += safesetid/<br>
obj-$(CONFIG_SECURITY_LOCKDOWN_LSM) += lockdown/<br>
-obj-$(CONFIG_CGROUP_DEVICE) += device_cgroup.o<br>
+obj-$(CONFIG_CGROUPS) += device_cgroup.o<br>
obj-$(CONFIG_BPF_LSM) += bpf/<br>
<br>
# Object integrity file lists<br>
diff --git a/security/device_cgroup.c b/security/device_cgroup.c<br>
index 7d0f8f7431ff..43ab0ad45c1b 100644<br>
--- a/security/device_cgroup.c<br>
+++ b/security/device_cgroup.c<br>
@@ -15,6 +15,8 @@<br>
#include <linux/rcupdate.h><br>
#include <linux/mutex.h><br>
<br>
+#ifdef CONFIG_CGROUP_DEVICE<br>
+<br>
static DEFINE_MUTEX(devcgroup_mutex);<br>
<br>
enum devcg_behavior {<br>
@@ -792,7 +794,7 @@ struct cgroup_subsys devices_cgrp_subsys = {<br>
};<br>
<br>
/**<br>
- * __devcgroup_check_permission - checks if an inode operation is permitted<br>
+ * devcgroup_legacy_check_permission - checks if an inode operation is permitted<br>
* @dev_cgroup: the dev cgroup to be tested against<br>
* @type: device type<br>
* @major: device major number<br>
@@ -801,7 +803,7 @@ struct cgroup_subsys devices_cgrp_subsys = {<br>
*<br>
* returns 0 on success, -EPERM case the operation is not permitted<br>
*/<br>
-static int __devcgroup_check_permission(short type, u32 major, u32 minor,<br>
+static int devcgroup_legacy_check_permission(short type, u32 major, u32 minor,<br>
short access)<br>
{<br>
struct dev_cgroup *dev_cgroup;<br>
@@ -825,6 +827,10 @@ static int __devcgroup_check_permission(short type, u32 major, u32 minor,<br>
return 0;<br>
}<br>
<br>
+#endif /* CONFIG_CGROUP_DEVICE */<br>
+<br>
+#if defined(CONFIG_CGROUP_DEVICE) || defined(CONFIG_CGROUP_BPF)<br>
+<br>
int devcgroup_check_permission(short type, u32 major, u32 minor, short access)<br>
{<br>
int rc = BPF_CGROUP_RUN_PROG_DEVICE_CGROUP(type, major, minor, access);<br>
@@ -832,6 +838,13 @@ int devcgroup_check_permission(short type, u32 major, u32 minor, short access)<br>
if (rc)<br>
return -EPERM;<br>
<br>
- return __devcgroup_check_permission(type, major, minor, access);<br>
+ #ifdef CONFIG_CGROUP_DEVICE<br>
+ return devcgroup_legacy_check_permission(type, major, minor, access);<br>
+<br>
+ #else /* CONFIG_CGROUP_DEVICE */<br>
+ return 0;<br>
+<br>
+ #endif /* CONFIG_CGROUP_DEVICE */<br>
}<br>
EXPORT_SYMBOL(devcgroup_check_permission);<br>
+#endif /* defined(CONFIG_CGROUP_DEVICE) || defined(CONFIG_CGROUP_BPF) */<br>
-- <br>
2.26.0<br>
<br>
</blockquote></div>