Skip to content

Commit ecf8fec

Browse files
Roman Gushchindavem330
authored andcommitted
device_cgroup: prepare code for bpf-based device controller
This is non-functional change to prepare the device cgroup code for adding eBPF-based controller for cgroups v2. The patch performs the following changes: 1) __devcgroup_inode_permission() and devcgroup_inode_mknod() are moving to the device-cgroup.h and converting into static inline. 2) __devcgroup_check_permission() is exported. 3) devcgroup_check_permission() wrapper is introduced to be used by both existing and new bpf-based implementations. Signed-off-by: Roman Gushchin <guro@fb.com> Acked-by: Tejun Heo <tj@kernel.org> Acked-by: Alexei Starovoitov <ast@kernel.org> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 67e306f commit ecf8fec

2 files changed

Lines changed: 59 additions & 49 deletions

File tree

‎include/linux/device_cgroup.h‎

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,70 @@
11
/* SPDX-License-Identifier: GPL-2.0 */
22
#include <linux/fs.h>
33

4+
#define DEVCG_ACC_MKNOD 1
5+
#define DEVCG_ACC_READ 2
6+
#define DEVCG_ACC_WRITE 4
7+
#define DEVCG_ACC_MASK (DEVCG_ACC_MKNOD | DEVCG_ACC_READ | DEVCG_ACC_WRITE)
8+
9+
#define DEVCG_DEV_BLOCK 1
10+
#define DEVCG_DEV_CHAR 2
11+
#define DEVCG_DEV_ALL 4 /* this represents all devices */
12+
13+
#ifdef CONFIG_CGROUP_DEVICE
14+
extern int __devcgroup_check_permission(short type, u32 major, u32 minor,
15+
short access);
16+
#else
17+
static inline int __devcgroup_check_permission(short type, u32 major, u32 minor,
18+
short access)
19+
{ return 0; }
20+
#endif
21+
422
#ifdef CONFIG_CGROUP_DEVICE
5-
extern int __devcgroup_inode_permission(struct inode *inode, int mask);
6-
extern int devcgroup_inode_mknod(int mode, dev_t dev);
23+
static inline int devcgroup_check_permission(short type, u32 major, u32 minor,
24+
short access)
25+
{
26+
return __devcgroup_check_permission(type, major, minor, access);
27+
}
28+
729
static inline int devcgroup_inode_permission(struct inode *inode, int mask)
830
{
31+
short type, access = 0;
32+
933
if (likely(!inode->i_rdev))
1034
return 0;
11-
if (!S_ISBLK(inode->i_mode) && !S_ISCHR(inode->i_mode))
35+
36+
if (S_ISBLK(inode->i_mode))
37+
type = DEVCG_DEV_BLOCK;
38+
else if (S_ISCHR(inode->i_mode))
39+
type = DEVCG_DEV_CHAR;
40+
else
1241
return 0;
13-
return __devcgroup_inode_permission(inode, mask);
42+
43+
if (mask & MAY_WRITE)
44+
access |= DEVCG_ACC_WRITE;
45+
if (mask & MAY_READ)
46+
access |= DEVCG_ACC_READ;
47+
48+
return devcgroup_check_permission(type, imajor(inode), iminor(inode),
49+
access);
1450
}
51+
52+
static inline int devcgroup_inode_mknod(int mode, dev_t dev)
53+
{
54+
short type;
55+
56+
if (!S_ISBLK(mode) && !S_ISCHR(mode))
57+
return 0;
58+
59+
if (S_ISBLK(mode))
60+
type = DEVCG_DEV_BLOCK;
61+
else
62+
type = DEVCG_DEV_CHAR;
63+
64+
return devcgroup_check_permission(type, MAJOR(dev), MINOR(dev),
65+
DEVCG_ACC_MKNOD);
66+
}
67+
1568
#else
1669
static inline int devcgroup_inode_permission(struct inode *inode, int mask)
1770
{ return 0; }

‎security/device_cgroup.c‎

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

18-
#define DEVCG_ACC_MKNOD 1
19-
#define DEVCG_ACC_READ 2
20-
#define DEVCG_ACC_WRITE 4
21-
#define DEVCG_ACC_MASK (DEVCG_ACC_MKNOD | DEVCG_ACC_READ | DEVCG_ACC_WRITE)
22-
23-
#define DEVCG_DEV_BLOCK 1
24-
#define DEVCG_DEV_CHAR 2
25-
#define DEVCG_DEV_ALL 4 /* this represents all devices */
26-
2718
static DEFINE_MUTEX(devcgroup_mutex);
2819

2920
enum devcg_behavior {
@@ -810,8 +801,8 @@ struct cgroup_subsys devices_cgrp_subsys = {
810801
*
811802
* returns 0 on success, -EPERM case the operation is not permitted
812803
*/
813-
static int __devcgroup_check_permission(short type, u32 major, u32 minor,
814-
short access)
804+
int __devcgroup_check_permission(short type, u32 major, u32 minor,
805+
short access)
815806
{
816807
struct dev_cgroup *dev_cgroup;
817808
bool rc;
@@ -833,37 +824,3 @@ static int __devcgroup_check_permission(short type, u32 major, u32 minor,
833824

834825
return 0;
835826
}
836-
837-
int __devcgroup_inode_permission(struct inode *inode, int mask)
838-
{
839-
short type, access = 0;
840-
841-
if (S_ISBLK(inode->i_mode))
842-
type = DEVCG_DEV_BLOCK;
843-
if (S_ISCHR(inode->i_mode))
844-
type = DEVCG_DEV_CHAR;
845-
if (mask & MAY_WRITE)
846-
access |= DEVCG_ACC_WRITE;
847-
if (mask & MAY_READ)
848-
access |= DEVCG_ACC_READ;
849-
850-
return __devcgroup_check_permission(type, imajor(inode), iminor(inode),
851-
access);
852-
}
853-
854-
int devcgroup_inode_mknod(int mode, dev_t dev)
855-
{
856-
short type;
857-
858-
if (!S_ISBLK(mode) && !S_ISCHR(mode))
859-
return 0;
860-
861-
if (S_ISBLK(mode))
862-
type = DEVCG_DEV_BLOCK;
863-
else
864-
type = DEVCG_DEV_CHAR;
865-
866-
return __devcgroup_check_permission(type, MAJOR(dev), MINOR(dev),
867-
DEVCG_ACC_MKNOD);
868-
869-
}

0 commit comments

Comments
 (0)