1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2023 Huawei Device Co., Ltd.
4 */
5
6 #ifndef _LINUX_CED_DETECTION_POINTS_H
7 #define _LINUX_CED_DETECTION_POINTS_H
8
9 #include <linux/slab.h>
10 #include <linux/pid.h>
11 #include <linux/sched.h>
12 #include <linux/uidgid.h>
13 #include <linux/capability.h>
14 #include <linux/cred.h>
15 #include <linux/fs_struct.h>
16 #include <linux/path.h>
17 #include <linux/nsproxy.h>
18
19 struct cred_info {
20 uid_t euid;
21 gid_t egid;
22 uid_t fsuid;
23 kernel_cap_t cap_effective;
24 };
25
cred_info_record(struct cred_info *info, const struct cred *cred)26 static inline void cred_info_record(struct cred_info *info, const struct cred *cred)
27 {
28 info->euid = cred->euid.val;
29 info->egid = cred->egid.val;
30 info->fsuid = cred->fsuid.val;
31
32 memcpy(&info->cap_effective, &cred->cap_effective, sizeof(kernel_cap_t));
33 }
34
35 struct ns_info {
36 struct mnt_namespace *mnt_ns;
37 struct pid_namespace *pid_ns;
38 struct net *net_ns;
39 };
40
ns_info_record(struct ns_info *info, const struct nsproxy *nsproxy)41 static inline void ns_info_record(struct ns_info *info, const struct nsproxy *nsproxy)
42 {
43 if (nsproxy) {
44 info->mnt_ns = nsproxy->mnt_ns;
45 info->pid_ns = nsproxy->pid_ns_for_children;
46 info->net_ns = nsproxy->net_ns;
47 }
48 }
49
50 struct process_info {
51 struct cred_info cred;
52 struct ns_info ns;
53 };
54
55 struct point_info {
56 struct rb_node node;
57 pid_t tgid;
58 uint32_t count;
59 struct process_info *info;
60 };
61
process_info_record(struct task_struct *task)62 static inline struct process_info *process_info_record(struct task_struct *task)
63 {
64 struct process_info *info = NULL;
65 const struct cred *cred = get_task_cred(task);
66 if (cred == NULL) {
67 return NULL;
68 }
69
70 info = kmalloc(sizeof(struct process_info), GFP_KERNEL);
71 if (info == NULL) {
72 return NULL;
73 }
74 memset(info, 0, sizeof(struct process_info));
75
76 cred_info_record(&info->cred, cred);
77
78 if (task->nsproxy != NULL) {
79 ns_info_record(&info->ns, task->nsproxy);
80 }
81
82 return info;
83 }
84
85 #endif /* _LINUX_CED_DETECTION_POINTS_H */