Skip to content

Commit eec8fd0

Browse files
odinugehtejun
authored andcommitted
device_cgroup: Cleanup cgroup eBPF device filter code
Original cgroup v2 eBPF code for filtering device access made it possible to compile with CONFIG_CGROUP_DEVICE=n and still use the eBPF filtering. Change commit 4b7d4d4 ("device_cgroup: Export devcgroup_check_permission") reverted this, making it required to set it to y. Since the device filtering (and all the docs) for cgroup v2 is no longer a "device controller" like it was in v1, someone might compile their kernel with CONFIG_CGROUP_DEVICE=n. Then (for linux 5.5+) the eBPF filter will not be invoked, and all processes will be allowed access to all devices, no matter what the eBPF filter says. Signed-off-by: Odin Ugedal <[email protected]> Acked-by: Roman Gushchin <[email protected]> Signed-off-by: Tejun Heo <[email protected]>
1 parent 772b314 commit eec8fd0

File tree

4 files changed

+23
-14
lines changed

4 files changed

+23
-14
lines changed

drivers/gpu/drm/amd/amdkfd/kfd_priv.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1050,7 +1050,7 @@ void kfd_dec_compute_active(struct kfd_dev *dev);
10501050
/* Check with device cgroup if @kfd device is accessible */
10511051
static inline int kfd_devcgroup_check_permission(struct kfd_dev *kfd)
10521052
{
1053-
#if defined(CONFIG_CGROUP_DEVICE)
1053+
#if defined(CONFIG_CGROUP_DEVICE) || defined(CONFIG_CGROUP_BPF)
10541054
struct drm_device *ddev = kfd->ddev;
10551055

10561056
return devcgroup_check_permission(DEVCG_DEV_CHAR, ddev->driver->major,

include/linux/device_cgroup.h

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/* SPDX-License-Identifier: GPL-2.0 */
22
#include <linux/fs.h>
3-
#include <linux/bpf-cgroup.h>
43

54
#define DEVCG_ACC_MKNOD 1
65
#define DEVCG_ACC_READ 2
@@ -11,16 +10,10 @@
1110
#define DEVCG_DEV_CHAR 2
1211
#define DEVCG_DEV_ALL 4 /* this represents all devices */
1312

14-
#ifdef CONFIG_CGROUP_DEVICE
15-
int devcgroup_check_permission(short type, u32 major, u32 minor,
16-
short access);
17-
#else
18-
static inline int devcgroup_check_permission(short type, u32 major, u32 minor,
19-
short access)
20-
{ return 0; }
21-
#endif
2213

2314
#if defined(CONFIG_CGROUP_DEVICE) || defined(CONFIG_CGROUP_BPF)
15+
int devcgroup_check_permission(short type, u32 major, u32 minor,
16+
short access);
2417
static inline int devcgroup_inode_permission(struct inode *inode, int mask)
2518
{
2619
short type, access = 0;
@@ -61,6 +54,9 @@ static inline int devcgroup_inode_mknod(int mode, dev_t dev)
6154
}
6255

6356
#else
57+
static inline int devcgroup_check_permission(short type, u32 major, u32 minor,
58+
short access)
59+
{ return 0; }
6460
static inline int devcgroup_inode_permission(struct inode *inode, int mask)
6561
{ return 0; }
6662
static inline int devcgroup_inode_mknod(int mode, dev_t dev)

security/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ obj-$(CONFIG_SECURITY_YAMA) += yama/
3030
obj-$(CONFIG_SECURITY_LOADPIN) += loadpin/
3131
obj-$(CONFIG_SECURITY_SAFESETID) += safesetid/
3232
obj-$(CONFIG_SECURITY_LOCKDOWN_LSM) += lockdown/
33-
obj-$(CONFIG_CGROUP_DEVICE) += device_cgroup.o
33+
obj-$(CONFIG_CGROUPS) += device_cgroup.o
3434
obj-$(CONFIG_BPF_LSM) += bpf/
3535

3636
# Object integrity file lists

security/device_cgroup.c

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
#include <linux/rcupdate.h>
1616
#include <linux/mutex.h>
1717

18+
#ifdef CONFIG_CGROUP_DEVICE
19+
1820
static DEFINE_MUTEX(devcgroup_mutex);
1921

2022
enum devcg_behavior {
@@ -792,7 +794,7 @@ struct cgroup_subsys devices_cgrp_subsys = {
792794
};
793795

794796
/**
795-
* __devcgroup_check_permission - checks if an inode operation is permitted
797+
* devcgroup_legacy_check_permission - checks if an inode operation is permitted
796798
* @dev_cgroup: the dev cgroup to be tested against
797799
* @type: device type
798800
* @major: device major number
@@ -801,7 +803,7 @@ struct cgroup_subsys devices_cgrp_subsys = {
801803
*
802804
* returns 0 on success, -EPERM case the operation is not permitted
803805
*/
804-
static int __devcgroup_check_permission(short type, u32 major, u32 minor,
806+
static int devcgroup_legacy_check_permission(short type, u32 major, u32 minor,
805807
short access)
806808
{
807809
struct dev_cgroup *dev_cgroup;
@@ -825,13 +827,24 @@ static int __devcgroup_check_permission(short type, u32 major, u32 minor,
825827
return 0;
826828
}
827829

830+
#endif /* CONFIG_CGROUP_DEVICE */
831+
832+
#if defined(CONFIG_CGROUP_DEVICE) || defined(CONFIG_CGROUP_BPF)
833+
828834
int devcgroup_check_permission(short type, u32 major, u32 minor, short access)
829835
{
830836
int rc = BPF_CGROUP_RUN_PROG_DEVICE_CGROUP(type, major, minor, access);
831837

832838
if (rc)
833839
return -EPERM;
834840

835-
return __devcgroup_check_permission(type, major, minor, access);
841+
#ifdef CONFIG_CGROUP_DEVICE
842+
return devcgroup_legacy_check_permission(type, major, minor, access);
843+
844+
#else /* CONFIG_CGROUP_DEVICE */
845+
return 0;
846+
847+
#endif /* CONFIG_CGROUP_DEVICE */
836848
}
837849
EXPORT_SYMBOL(devcgroup_check_permission);
850+
#endif /* defined(CONFIG_CGROUP_DEVICE) || defined(CONFIG_CGROUP_BPF) */

0 commit comments

Comments
 (0)