Skip to content

Commit 58ede6e

Browse files
committed
implement userspace
1 parent 6da48a0 commit 58ede6e

11 files changed

Lines changed: 124 additions & 25 deletions

File tree

kernel/core/init.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ int __init kernelsu_init(void)
140140
// with KSU SELinux domain before enforcing SELinux, so it
141141
// can continue to access /data/app etc. after enforcement.
142142
escape_to_root_for_init();
143-
ksu_process_tag_set(current, PROCESS_TAG_KSUD, "late_load");
143+
ksu_process_tag_set(current, PROCESS_TAG_KSUD, "");
144144

145145
ksu_allowlist_init();
146146
ksu_load_allow_list();

kernel/feature/process_tag.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,9 @@ static int process_tag_feature_set(u64 value)
256256
process_tag_lsm_hooked = true;
257257
pr_info("process_tag: initialized and enabled\n");
258258

259+
// mark the feature setter as ksud
260+
ksu_process_tag_set(current, PROCESS_TAG_KSUD, "");
261+
259262
return 0;
260263
}
261264

kernel/feature/process_tag.h

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,7 @@
66
#include <linux/rcupdate.h>
77
#include <linux/types.h>
88

9-
enum process_tag_type {
10-
PROCESS_TAG_NONE = 0,
11-
PROCESS_TAG_KSUD = 1,
12-
PROCESS_TAG_APP = 2,
13-
PROCESS_TAG_MODULE = 3,
14-
PROCESS_TAG_MANAGER = 4,
15-
};
9+
#include "uapi/process_tag.h"
1610

1711
struct process_tag {
1812
enum process_tag_type type;

uapi/ksu.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@
66
#include "uapi/feature.h"
77
#include "uapi/selinux.h"
88
#include "uapi/sulog.h"
9+
#include "uapi/process_tag.h"
910

1011
#endif // __KSU_UAPI_KSU_H

uapi/process_tag.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#ifndef __KSU_UAPI_PROCESS_TAG_H
2+
#define __KSU_UAPI_PROCESS_TAG_H
3+
4+
#include <linux/types.h>
5+
6+
enum process_tag_type : __u8 {
7+
PROCESS_TAG_NONE = 0,
8+
PROCESS_TAG_KSUD = 1,
9+
PROCESS_TAG_APP = 2,
10+
PROCESS_TAG_MODULE = 3,
11+
PROCESS_TAG_MANAGER = 4,
12+
};
13+
14+
#endif

uapi/supercall.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,13 @@ struct ksu_get_wrapper_fd_cmd {
110110
};
111111

112112
struct ksu_set_process_tag_cmd {
113-
__u8 type; /* Input: process_tag_type */
113+
__u8 type; /* Input: enum process_tag_type */
114114
char name[64]; /* Input: tag name (module name / package name) */
115115
};
116116

117117
struct ksu_get_process_tag_cmd {
118118
__u32 pid; /* Input: target process pid */
119-
__u8 type; /* Output: process_tag_type, PROCESS_TAG_NONE if no tag */
119+
__u8 type; /* Output: enum process_tag_type, PROCESS_TAG_NONE if no tag */
120120
char name[64]; /* Output: tag name */
121121
};
122122

userspace/ksud/src/cli.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,12 @@ enum Debug {
214214

215215
/// Launch sulogd daemon manually
216216
Sulogd,
217+
218+
/// Process tag management
219+
Tag {
220+
#[command(subcommand)]
221+
command: ProcessTagCommand,
222+
}
217223
}
218224

219225
#[derive(clap::Subcommand, Debug)]
@@ -243,6 +249,16 @@ enum MarkCommand {
243249
Refresh,
244250
}
245251

252+
253+
#[derive(clap::Subcommand, Debug)]
254+
enum ProcessTagCommand {
255+
/// Get mark status for a process
256+
Get {
257+
/// target pid (0 for total count)
258+
pid: u32,
259+
}
260+
}
261+
246262
#[derive(clap::Subcommand, Debug)]
247263
enum Sepolicy {
248264
/// Patch sepolicy
@@ -700,6 +716,9 @@ pub fn run() -> Result<()> {
700716
MarkCommand::Refresh => debug::mark_refresh(),
701717
},
702718
Debug::Sulogd => sulog::ensure_sulogd_running(),
719+
Debug::Tag { command } => match command {
720+
ProcessTagCommand::Get { pid } => debug::get_tag(pid),
721+
}
703722
},
704723

705724
Commands::BootPatch(boot_patch) => crate::boot_patch::patch(boot_patch),

userspace/ksud/src/debug.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::{
66
process::Command,
77
};
88

9-
use crate::ksucalls;
9+
use crate::{ksu_uapi, ksucalls};
1010

1111
const KERNEL_PARAM_PATH: &str = "/sys/module/kernelsu";
1212

@@ -107,3 +107,20 @@ pub fn mark_refresh() -> Result<()> {
107107
println!("Refreshed mark for all running processes");
108108
Ok(())
109109
}
110+
111+
pub fn get_tag(pid: u32) -> Result<()> {
112+
let tag = ksucalls::get_process_tag(pid)?;
113+
114+
let tag_type = match tag.tag_type {
115+
ksu_uapi::process_tag_type_PROCESS_TAG_NONE => "none".to_owned(),
116+
ksu_uapi::process_tag_type_PROCESS_TAG_APP => "app".to_owned(),
117+
ksu_uapi::process_tag_type_PROCESS_TAG_KSUD => "ksud".to_owned(),
118+
ksu_uapi::process_tag_type_PROCESS_TAG_MODULE => "module".to_owned(),
119+
ksu_uapi::process_tag_type_PROCESS_TAG_MANAGER => "manager".to_owned(),
120+
_ => format!("{}", tag.tag_type),
121+
};
122+
123+
println!("pid: {pid} type={tag_type} name={}", tag.name);
124+
125+
Ok(())
126+
}

userspace/ksud/src/feature.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ pub enum FeatureId {
2020
KernelUmount = 1,
2121
Sulog = 2,
2222
AdbRoot = 3,
23+
ProcessTag = 4,
2324
}
2425

2526
impl FeatureId {
@@ -29,6 +30,7 @@ impl FeatureId {
2930
1 => Some(Self::KernelUmount),
3031
2 => Some(Self::Sulog),
3132
3 => Some(Self::AdbRoot),
33+
4 => Some(Self::ProcessTag),
3234
_ => None,
3335
}
3436
}
@@ -39,6 +41,7 @@ impl FeatureId {
3941
Self::KernelUmount => "kernel_umount",
4042
Self::Sulog => "sulog",
4143
Self::AdbRoot => "adb_root",
44+
Self::ProcessTag => "process_tag",
4245
}
4346
}
4447

@@ -54,6 +57,7 @@ impl FeatureId {
5457
"SU Log - streams kernel sulog events to userspace and persists them to disk"
5558
}
5659
Self::AdbRoot => "ADB Root - Enable adbd root",
60+
Self::ProcessTag => "Process Tag - Tagging process for audit",
5761
}
5862
}
5963
}
@@ -64,6 +68,7 @@ fn parse_feature_id(name: &str) -> Result<FeatureId> {
6468
"kernel_umount" | "1" => Ok(FeatureId::KernelUmount),
6569
"sulog" | "2" => Ok(FeatureId::Sulog),
6670
"adb_root" | "3" => Ok(FeatureId::AdbRoot),
71+
"process_tag" | "4" => Ok(FeatureId::ProcessTag),
6772
_ => bail!("Unknown feature: {name}"),
6873
}
6974
}

userspace/ksud/src/ksucalls.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,3 +249,39 @@ pub fn set_init_pgrp() -> std::io::Result<()> {
249249
)?;
250250
Ok(())
251251
}
252+
253+
pub fn set_module_tag(tag: &str) -> std::io::Result<()> {
254+
let mut cmd = ksu_uapi::ksu_set_process_tag_cmd {
255+
type_: ksu_uapi::process_tag_type_PROCESS_TAG_MODULE,
256+
name: [0; 64]
257+
};
258+
let bytes = tag.as_bytes();
259+
let len = bytes.len().max(63);
260+
cmd.name[..len].copy_from_slice(&bytes[..len]);
261+
ksuctl(
262+
ksu_uapi::KSU_IOCTL_SET_PROCESS_TAG,
263+
&raw mut cmd,
264+
)?;
265+
Ok(())
266+
}
267+
268+
pub struct ProcessTag {
269+
pub tag_type: u8,
270+
pub name: String,
271+
}
272+
273+
pub fn get_process_tag(pid: u32) -> anyhow::Result<ProcessTag> {
274+
let mut cmd = ksu_uapi::ksu_get_process_tag_cmd {
275+
pid,
276+
type_: ksu_uapi::process_tag_type_PROCESS_TAG_NONE,
277+
name: [0; 64]
278+
};
279+
ksuctl(
280+
ksu_uapi::KSU_IOCTL_GET_PROCESS_TAG,
281+
&raw mut cmd,
282+
)?;
283+
Ok(ProcessTag {
284+
tag_type: cmd.type_,
285+
name: std::ffi::CString::new(cmd.name)?.into_string()?
286+
})
287+
}

0 commit comments

Comments
 (0)