blob: fa35b52e0002e03a4a2476f2f854daade6e6742d [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -07002#include <linux/fs.h>
Roman Gushchinebc614f2017-11-05 08:15:32 -05003#include <linux/bpf-cgroup.h>
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -07004
Roman Gushchinecf8fec2017-11-05 08:15:31 -05005#define DEVCG_ACC_MKNOD 1
6#define DEVCG_ACC_READ 2
7#define DEVCG_ACC_WRITE 4
8#define DEVCG_ACC_MASK (DEVCG_ACC_MKNOD | DEVCG_ACC_READ | DEVCG_ACC_WRITE)
9
10#define DEVCG_DEV_BLOCK 1
11#define DEVCG_DEV_CHAR 2
12#define DEVCG_DEV_ALL 4 /* this represents all devices */
13
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -070014#ifdef CONFIG_CGROUP_DEVICE
Harish Kasiviswanathan4b7d4d42019-05-16 11:37:16 -040015int devcgroup_check_permission(short type, u32 major, u32 minor,
16 short access);
Roman Gushchinecf8fec2017-11-05 08:15:31 -050017#else
Harish Kasiviswanathan4b7d4d42019-05-16 11:37:16 -040018static inline int devcgroup_check_permission(short type, u32 major, u32 minor,
19 short access)
Roman Gushchinecf8fec2017-11-05 08:15:31 -050020{ return 0; }
21#endif
22
Roman Gushchinebc614f2017-11-05 08:15:32 -050023#if defined(CONFIG_CGROUP_DEVICE) || defined(CONFIG_CGROUP_BPF)
Al Viro482e0cd2011-06-19 13:01:04 -040024static inline int devcgroup_inode_permission(struct inode *inode, int mask)
25{
Roman Gushchinecf8fec2017-11-05 08:15:31 -050026 short type, access = 0;
27
Al Viro482e0cd2011-06-19 13:01:04 -040028 if (likely(!inode->i_rdev))
29 return 0;
Roman Gushchinecf8fec2017-11-05 08:15:31 -050030
31 if (S_ISBLK(inode->i_mode))
32 type = DEVCG_DEV_BLOCK;
33 else if (S_ISCHR(inode->i_mode))
34 type = DEVCG_DEV_CHAR;
35 else
Al Viro482e0cd2011-06-19 13:01:04 -040036 return 0;
Roman Gushchinecf8fec2017-11-05 08:15:31 -050037
38 if (mask & MAY_WRITE)
39 access |= DEVCG_ACC_WRITE;
40 if (mask & MAY_READ)
41 access |= DEVCG_ACC_READ;
42
43 return devcgroup_check_permission(type, imajor(inode), iminor(inode),
44 access);
Al Viro482e0cd2011-06-19 13:01:04 -040045}
Roman Gushchinecf8fec2017-11-05 08:15:31 -050046
47static inline int devcgroup_inode_mknod(int mode, dev_t dev)
48{
49 short type;
50
51 if (!S_ISBLK(mode) && !S_ISCHR(mode))
52 return 0;
53
54 if (S_ISBLK(mode))
55 type = DEVCG_DEV_BLOCK;
56 else
57 type = DEVCG_DEV_CHAR;
58
59 return devcgroup_check_permission(type, MAJOR(dev), MINOR(dev),
60 DEVCG_ACC_MKNOD);
61}
62
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -070063#else
64static inline int devcgroup_inode_permission(struct inode *inode, int mask)
65{ return 0; }
66static inline int devcgroup_inode_mknod(int mode, dev_t dev)
67{ return 0; }
68#endif