Skip to content

Commit 251c3c3

Browse files
committed
Remove old ffi data structure
1 parent cd0eca2 commit 251c3c3

3 files changed

Lines changed: 97 additions & 93 deletions

File tree

native/src/core/lib.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -125,15 +125,6 @@ pub mod ffi {
125125
gids: Vec<u32>,
126126
}
127127

128-
struct SuAppRequest<'a> {
129-
uid: i32,
130-
pid: i32,
131-
eval_uid: i32,
132-
mgr_pkg: &'a str,
133-
mgr_uid: i32,
134-
request: &'a SuRequest,
135-
}
136-
137128
unsafe extern "C++" {
138129
#[namespace = "rust"]
139130
#[cxx_name = "Utf8CStr"]

native/src/core/su/connect.rs

Lines changed: 80 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
use super::SuInfo;
2+
use super::db::RootSettings;
13
use crate::consts::{INTERNAL_DIR, MAGISK_FILE_CON};
2-
use crate::daemon::{MagiskD, to_user_id};
3-
use crate::ffi::{SuAppRequest, SuPolicy, get_magisk_tmp};
4+
use crate::daemon::to_user_id;
5+
use crate::ffi::{SuPolicy, SuRequest, get_magisk_tmp};
6+
use crate::socket::IpcRead;
47
use ExtraVal::{Bool, Int, IntList, Str};
58
use base::{
6-
BytesExt, FileAttr, LibcReturn, LoggedResult, OsError, ResultExt, cstr, fork_dont_care, info,
7-
libc,
9+
BytesExt, FileAttr, LibcReturn, LoggedResult, OsError, ResultExt, cstr, fork_dont_care, libc,
810
};
911
use libc::pollfd as PollFd;
1012
use num_traits::AsPrimitive;
@@ -79,19 +81,21 @@ impl Extra {
7981
}
8082
}
8183

82-
impl MagiskD {
83-
fn exec_cmd(
84-
&self,
85-
action: &'static str,
86-
extras: &[Extra],
87-
info: &SuAppRequest,
88-
use_provider: bool,
89-
) {
90-
let user = to_user_id(info.eval_uid);
84+
pub(super) struct SuAppContext<'a> {
85+
pub(super) cred: libc::ucred,
86+
pub(super) request: &'a SuRequest,
87+
pub(super) info: &'a SuInfo,
88+
pub(super) settings: &'a mut RootSettings,
89+
pub(super) sdk_int: i32,
90+
}
91+
92+
impl SuAppContext<'_> {
93+
fn exec_cmd(&self, action: &'static str, extras: &[Extra], use_provider: bool) {
94+
let user = to_user_id(self.info.eval_uid);
9195
let user = user.to_string();
9296

9397
if use_provider {
94-
let provider = format!("content://{}.provider", info.mgr_pkg);
98+
let provider = format!("content://{}.provider", self.info.mgr_pkg);
9599
let mut cmd = Command::new("/system/bin/app_process");
96100
cmd.args([
97101
"/system/bin",
@@ -104,7 +108,7 @@ impl MagiskD {
104108
"--method",
105109
action,
106110
]);
107-
if self.sdk_int() >= 30 {
111+
if self.sdk_int >= 30 {
108112
extras.iter().for_each(|e| e.add_bind(&mut cmd))
109113
} else {
110114
extras.iter().for_each(|e| e.add_bind_legacy(&mut cmd))
@@ -126,7 +130,7 @@ impl MagiskD {
126130
"com.android.commands.am.Am",
127131
"start",
128132
"-p",
129-
info.mgr_pkg,
133+
&self.info.mgr_pkg,
130134
"--user",
131135
&user,
132136
"-a",
@@ -152,21 +156,21 @@ impl MagiskD {
152156
}
153157
}
154158

155-
pub fn app_request(&self, info: &SuAppRequest) -> LoggedResult<File> {
159+
fn app_request(&mut self) {
156160
let mut fifo = cstr::buf::new::<64>();
157161
fifo.write_fmt(format_args!(
158162
"{}/{}/su_request_{}",
159163
get_magisk_tmp(),
160164
INTERNAL_DIR,
161-
info.pid
165+
self.cred.pid
162166
))
163167
.ok();
164168

165169
let fd: LoggedResult<File> = try {
166170
let mut attr = FileAttr::new();
167171
attr.st.st_mode = 0o600;
168-
attr.st.st_uid = info.mgr_uid.as_();
169-
attr.st.st_gid = info.mgr_uid.as_();
172+
attr.st.st_uid = self.info.mgr_uid.as_();
173+
attr.st.st_gid = self.info.mgr_uid.as_();
170174
attr.con.write_str(MAGISK_FILE_CON).ok();
171175

172176
fifo.mkfifo(0o600)?;
@@ -179,14 +183,14 @@ impl MagiskD {
179183
},
180184
Extra {
181185
key: "uid",
182-
value: Int(info.eval_uid),
186+
value: Int(self.info.eval_uid),
183187
},
184188
Extra {
185189
key: "pid",
186-
value: Int(info.pid),
190+
value: Int(self.cred.pid),
187191
},
188192
];
189-
self.exec_cmd("request", &extras, info, false);
193+
self.exec_cmd("request", &extras, false);
190194

191195
// Open with O_RDWR to prevent FIFO open block
192196
let fd = fifo.open(libc::O_RDWR | libc::O_CLOEXEC)?;
@@ -206,79 +210,106 @@ impl MagiskD {
206210
};
207211

208212
fifo.remove().log_ok();
209-
fd
213+
214+
if let Ok(mut fd) = fd {
215+
self.settings.policy = SuPolicy {
216+
repr: fd
217+
.read_decodable::<i32>()
218+
.log()
219+
.map(i32::from_be)
220+
.unwrap_or(SuPolicy::Deny.repr),
221+
};
222+
} else {
223+
self.settings.policy = SuPolicy::Deny;
224+
};
210225
}
211226

212-
pub fn app_notify(&self, info: &SuAppRequest, policy: SuPolicy) {
213-
if fork_dont_care() != 0 {
214-
return;
215-
}
227+
fn app_notify(&self) {
216228
let extras = [
217229
Extra {
218230
key: "from.uid",
219-
value: Int(info.uid),
231+
value: Int(self.cred.uid.as_()),
220232
},
221233
Extra {
222234
key: "pid",
223-
value: Int(info.pid),
235+
value: Int(self.cred.pid.as_()),
224236
},
225237
Extra {
226238
key: "policy",
227-
value: Int(policy.repr),
239+
value: Int(self.settings.policy.repr),
228240
},
229241
];
230-
self.exec_cmd("notify", &extras, info, true);
231-
exit(0);
242+
self.exec_cmd("notify", &extras, true);
232243
}
233244

234-
pub fn app_log(&self, info: &SuAppRequest, policy: SuPolicy, notify: bool) {
235-
if fork_dont_care() != 0 {
236-
return;
237-
}
238-
let command = if info.request.command.is_empty() {
239-
&info.request.shell
245+
fn app_log(&self) {
246+
let command = if self.request.command.is_empty() {
247+
&self.request.shell
240248
} else {
241-
&info.request.command
249+
&self.request.command
242250
};
243251
let extras = [
244252
Extra {
245253
key: "from.uid",
246-
value: Int(info.uid),
254+
value: Int(self.cred.uid.as_()),
247255
},
248256
Extra {
249257
key: "to.uid",
250-
value: Int(info.request.target_uid),
258+
value: Int(self.request.target_uid),
251259
},
252260
Extra {
253261
key: "pid",
254-
value: Int(info.pid),
262+
value: Int(self.cred.pid.as_()),
255263
},
256264
Extra {
257265
key: "policy",
258-
value: Int(policy.repr),
266+
value: Int(self.settings.policy.repr),
259267
},
260268
Extra {
261269
key: "target",
262-
value: Int(info.request.target_pid),
270+
value: Int(self.request.target_pid),
263271
},
264272
Extra {
265273
key: "context",
266-
value: Str(info.request.context.clone()),
274+
value: Str(self.request.context.clone()),
267275
},
268276
Extra {
269277
key: "gids",
270-
value: IntList(info.request.gids.clone()),
278+
value: IntList(self.request.gids.clone()),
271279
},
272280
Extra {
273281
key: "command",
274282
value: Str(command.clone()),
275283
},
276284
Extra {
277285
key: "notify",
278-
value: Bool(notify),
286+
value: Bool(self.settings.notify),
279287
},
280288
];
281-
self.exec_cmd("log", &extras, info, true);
289+
self.exec_cmd("log", &extras, true);
290+
}
291+
292+
pub(super) fn connect_app(&mut self) {
293+
// If policy is undetermined, show dialog for user consent
294+
if self.settings.policy == SuPolicy::Query {
295+
self.app_request();
296+
}
297+
298+
if !self.settings.log && !self.settings.notify {
299+
return;
300+
}
301+
302+
if fork_dont_care() != 0 {
303+
return;
304+
}
305+
306+
// Notify su usage to application
307+
if self.settings.log {
308+
self.app_log();
309+
} else if self.settings.notify {
310+
self.app_notify();
311+
}
312+
282313
exit(0);
283314
}
284315
}

native/src/core/su/daemon.rs

Lines changed: 17 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
use super::connect::SuAppContext;
2+
use super::db::RootSettings;
13
use crate::UCred;
24
use crate::daemon::{AID_ROOT, AID_SHELL, MagiskD, to_app_id, to_user_id};
35
use crate::db::{DbSettings, MultiuserMode, RootAccess};
4-
use crate::ffi::{SuAppRequest, SuPolicy, SuRequest, exec_root_shell};
6+
use crate::ffi::{SuPolicy, SuRequest, exec_root_shell};
57
use crate::socket::IpcRead;
6-
use crate::su::db::RootSettings;
78
use base::{LoggedResult, ResultExt, WriteExt, debug, error, exit_on_error, libc, warn};
89
use std::os::fd::{FromRawFd, IntoRawFd};
910
use std::os::unix::net::UnixStream;
@@ -29,11 +30,11 @@ impl Default for SuRequest {
2930
}
3031

3132
pub struct SuInfo {
32-
uid: i32,
33-
eval_uid: i32,
33+
pub(super) uid: i32,
34+
pub(super) eval_uid: i32,
35+
pub(super) mgr_pkg: String,
36+
pub(super) mgr_uid: i32,
3437
cfg: DbSettings,
35-
mgr_pkg: String,
36-
mgr_uid: i32,
3738
access: Mutex<AccessInfo>,
3839
}
3940

@@ -129,37 +130,18 @@ impl MagiskD {
129130
};
130131

131132
let info = self.get_su_info(cred.uid as i32);
132-
let app_req = SuAppRequest {
133-
uid: cred.uid as i32,
134-
pid: cred.pid,
135-
eval_uid: info.eval_uid,
136-
mgr_pkg: &info.mgr_pkg,
137-
mgr_uid: info.mgr_uid,
138-
request: &req,
139-
};
140-
141133
{
142134
let mut access = info.access.lock().unwrap();
143135

144-
if access.settings.policy == SuPolicy::Query {
145-
if let Ok(mut fd) = self.app_request(&app_req) {
146-
access.settings.policy = SuPolicy {
147-
repr: fd
148-
.read_decodable::<i32>()
149-
.log()
150-
.map(i32::from_be)
151-
.unwrap_or(SuPolicy::Deny.repr),
152-
};
153-
} else {
154-
access.settings.policy = SuPolicy::Deny;
155-
}
156-
}
157-
158-
if access.settings.log {
159-
self.app_log(&app_req, access.settings.policy, access.settings.notify);
160-
} else if access.settings.notify {
161-
self.app_notify(&app_req, access.settings.policy);
162-
}
136+
// Talk to su manager
137+
let mut app = SuAppContext {
138+
cred,
139+
request: &req,
140+
info: &info,
141+
settings: &mut access.settings,
142+
sdk_int: self.sdk_int(),
143+
};
144+
app.connect_app();
163145

164146
// Before unlocking, refresh the timestamp
165147
access.refresh();
@@ -286,9 +268,9 @@ impl MagiskD {
286268
Arc::new(SuInfo {
287269
uid,
288270
eval_uid,
289-
cfg,
290271
mgr_pkg,
291272
mgr_uid,
273+
cfg,
292274
access: Mutex::new(AccessInfo::new(access)),
293275
})
294276
};

0 commit comments

Comments
 (0)