| 1 | /* SPDX-License-Identifier: GPL-2.0 */ | 
|---|
| 2 | #include <linux/fs.h> | 
|---|
| 3 |  | 
|---|
| 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 |  | 
|---|
| 14 | #if defined(CONFIG_CGROUP_DEVICE) || defined(CONFIG_CGROUP_BPF) | 
|---|
| 15 | int devcgroup_check_permission(short type, u32 major, u32 minor, | 
|---|
| 16 | short access); | 
|---|
| 17 | static inline int devcgroup_inode_permission(struct inode *inode, int mask) | 
|---|
| 18 | { | 
|---|
| 19 | short type, access = 0; | 
|---|
| 20 |  | 
|---|
| 21 | if (likely(!S_ISBLK(inode->i_mode) && !S_ISCHR(inode->i_mode))) | 
|---|
| 22 | return 0; | 
|---|
| 23 |  | 
|---|
| 24 | if (likely(!inode->i_rdev)) | 
|---|
| 25 | return 0; | 
|---|
| 26 |  | 
|---|
| 27 | if (S_ISBLK(inode->i_mode)) | 
|---|
| 28 | type = DEVCG_DEV_BLOCK; | 
|---|
| 29 | else /* S_ISCHR by the test above */ | 
|---|
| 30 | type = DEVCG_DEV_CHAR; | 
|---|
| 31 |  | 
|---|
| 32 | if (mask & MAY_WRITE) | 
|---|
| 33 | access |= DEVCG_ACC_WRITE; | 
|---|
| 34 | if (mask & MAY_READ) | 
|---|
| 35 | access |= DEVCG_ACC_READ; | 
|---|
| 36 |  | 
|---|
| 37 | return devcgroup_check_permission(type, major: imajor(inode), minor: iminor(inode), | 
|---|
| 38 | access); | 
|---|
| 39 | } | 
|---|
| 40 |  | 
|---|
| 41 | static inline int devcgroup_inode_mknod(int mode, dev_t dev) | 
|---|
| 42 | { | 
|---|
| 43 | short type; | 
|---|
| 44 |  | 
|---|
| 45 | if (!S_ISBLK(mode) && !S_ISCHR(mode)) | 
|---|
| 46 | return 0; | 
|---|
| 47 |  | 
|---|
| 48 | if (S_ISCHR(mode) && dev == WHITEOUT_DEV) | 
|---|
| 49 | return 0; | 
|---|
| 50 |  | 
|---|
| 51 | if (S_ISBLK(mode)) | 
|---|
| 52 | type = DEVCG_DEV_BLOCK; | 
|---|
| 53 | else | 
|---|
| 54 | type = DEVCG_DEV_CHAR; | 
|---|
| 55 |  | 
|---|
| 56 | return devcgroup_check_permission(type, MAJOR(dev), MINOR(dev), | 
|---|
| 57 | DEVCG_ACC_MKNOD); | 
|---|
| 58 | } | 
|---|
| 59 |  | 
|---|
| 60 | #else | 
|---|
| 61 | static inline int devcgroup_check_permission(short type, u32 major, u32 minor, | 
|---|
| 62 | short access) | 
|---|
| 63 | { return 0; } | 
|---|
| 64 | static inline int devcgroup_inode_permission(struct inode *inode, int mask) | 
|---|
| 65 | { return 0; } | 
|---|
| 66 | static inline int devcgroup_inode_mknod(int mode, dev_t dev) | 
|---|
| 67 | { return 0; } | 
|---|
| 68 | #endif | 
|---|
| 69 |  | 
|---|