This repository has been archived by the owner on Sep 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
task_context.c
91 lines (79 loc) · 2.48 KB
/
task_context.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
* Copyright (c) 2023 Cisco and/or its affiliates. All rights reserved.
*
* SPDX-License-Identifier: MIT OR GPL-2.0-only
*
* Licensed under the MIT license <LICENSE.MIT or https://opensource.org/licenses/MIT> or the GPLv2 license
* <LICENSE.GPL or https://opensource.org/license/gpl-2-0>, at your option. This file may not be copied,
* modified, or distributed except according to those terms.
*/
#include <linux/uaccess.h>
#include <linux/file.h>
#include <linux/path.h>
#include <linux/dcache.h>
#include <linux/fs.h>
#include <linux/slab.h>
#include <linux/nsproxy.h>
#include <linux/cgroup.h>
#include <linux/ipc_namespace.h>
#include <linux/pid_namespace.h>
#include <linux/time_namespace.h>
#include <net/net_namespace.h>
#include <linux/utsname.h>
#include "task_context.h"
struct mnt_namespace
{
struct ns_common ns;
};
task_context *get_task_context(void)
{
struct task_context *context = kmalloc(sizeof(struct task_context), GFP_KERNEL);
strcpy(context->command_name, current->comm);
context->command_path = get_current_proc_path(context->command_path_buffer, sizeof(context->command_path_buffer));
current_uid_gid(&context->uid, &context->gid);
context->pid = current->pid;
// namespace ids
context->namespace_ids.uts = current->nsproxy->uts_ns->ns.inum;
context->namespace_ids.ipc = current->nsproxy->ipc_ns->ns.inum;
context->namespace_ids.mnt = current->nsproxy->mnt_ns->ns.inum;
context->namespace_ids.pid = current->nsproxy->pid_ns_for_children->ns.inum;
context->namespace_ids.net = current->nsproxy->net_ns->ns.inum;
context->namespace_ids.time = current->nsproxy->time_ns->ns.inum;
context->namespace_ids.cgroup = current->nsproxy->cgroup_ns->ns.inum;
struct css_set *cset = task_css_set(current);
if (cset)
{
cgroup_path(cset->dfl_cgrp, context->cgroup_path, sizeof(context->cgroup_path));
}
return context;
}
void free_task_context(struct task_context *context)
{
kfree(context);
}
char *get_current_proc_path(char *buf, int buflen)
{
struct file *exe_file;
char *result = ERR_PTR(-ENOENT);
struct mm_struct *mm;
mm = get_task_mm(current);
if (!mm)
{
goto out;
}
exe_file = mm->exe_file;
if (exe_file)
{
get_file(exe_file);
path_get(&exe_file->f_path);
}
mmput(mm);
if (exe_file)
{
result = d_path(&exe_file->f_path, buf, buflen);
path_put(&exe_file->f_path);
fput(exe_file);
}
out:
return result;
}