pai 0.1.11

Process Analyzer and Instrumenter
Documentation
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
//! A client can use [Args] object to control behaviour in trace controllers
//! components.
use crate::api::messages::Wait;
use crate::utils::process::Tid;
use crate::{Result, TargetPtr};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

use super::messages::{RegEvent, Stop};

#[derive(Default, Debug)]
struct Breakpoint {
	// numhit: usize,
}

/// Decide behaviour when syscall is detected and how much enrichment of
/// arguments to perform.
#[derive(Default, Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub enum Enrich {
	/// This is called None, but it does do some parsing:
	///
	/// - Detect system call name
	/// - Detect number of arguments
	/// - Find name of argument
	#[default]
	None,

	/// In addition to steps taken in [Enrich::None], also:
	///
	/// - Annotate with metadata about argument
	/// - Find out which flag values int resolves to
	Basic,

	/// In addition to steps taken in [Enrich::Basic], also:
	///
	/// - Read data from pointers to construct strings, objects, integers, etc.
	Full,
}
impl std::str::FromStr for Enrich {
	type Err = crate::Error;

	fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
		match s.to_lowercase().as_str() {
			"none" => Ok(Self::None),
			"basic" => Ok(Self::Basic),
			"full" => Ok(Self::Full),
			_ => Err(crate::Error::NotFound),
		}
	}
}
impl std::fmt::Display for Enrich {
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		f.write_fmt(format_args!("{:?}", self))
	}
}

macro_rules! bool_access {
	($name:ident) => {
		pub fn $name(&self, tid: Tid) -> bool {
			if let Some(args) = self.threads.get(&tid) {
				args.$name
			} else {
				self.global.$name
			}
		}
	};
}

macro_rules! builder_set_bool {
	($name:ident) => {
		pub fn $name(mut self) -> Self {
			self.args.$name = true;
			self.dirty = true;
			self
		}
		paste::paste! {
			pub fn [<set_ $name>](&mut self, value: bool) {
				self.dirty = true;
				self.args.$name = value;
			}
		}
	};
}

// use paste::paste;
macro_rules! builder_push_val {
	($name:ident, $arg:ident, $argtype:tt) => {
		paste::paste! {
			pub fn [<push_ $name>](mut self, $arg: $argtype) -> Self {
				log::trace!("pusing {:?}", $arg);
				if !self.args.$name.contains(&$arg) {
					self.dirty = true;
					self.args.$name.push($arg);
				}
				self
			}
		}
		paste::paste! {
			pub fn [<add_ $name>](&mut self, $arg: $argtype) {
				log::trace!("pusing {:?}", $arg);
				if !self.args.$name.contains(&$arg) {
					self.dirty = true;
					self.args.$name.push($arg);
				}
			}
		}
		paste::paste! {
			pub fn [<remove_ $name>](&mut self, $arg: $argtype) {
				log::trace!("pusing {:?}", $arg);
				if let Some(index) = self.args.$name.iter().position(|x| *x == $arg) {
					self.args.$name.remove(index);
					self.dirty = true;
				}
			}
		}
	};
}

/// Construct [Args]
///
/// ## Example
///
/// Below is an example to intercept all system calls.
///
/// ```rust
/// use pai::api::ArgsBuilder;
/// let args = ArgsBuilder::new()
///   .handle_steps()
///   .finish()
///   .expect("unable to construct args");
///
/// ```
#[derive(Default, Clone)]
pub struct ArgsBuilder {
	args: Args,
	dirty: bool,
}
impl ArgsBuilder {
	pub fn new() -> Self {
		Self::default()
	}
	pub fn new_dirty() -> Self {
		Self {
			args: Args::default(),
			dirty: true,
		}
	}
	pub fn finish(self) -> Result<Args> {
		self.args.sanity_check()?;
		Ok(self.args)
	}
	pub fn borrow_finish(&mut self) -> Result<Args> {
		let args = self.args.clone();
		args.sanity_check()?;
		self.dirty = false;
		Ok(args)
	}
	pub fn is_dirty(&self) -> bool {
		self.dirty
	}
	pub fn enrich_default(mut self, enrich: Enrich) -> Self {
		if enrich != self.args.enrich_default {
			self.args.enrich_default = enrich;
			self.dirty = true;
		}
		self
	}
	pub(crate) fn set_enrich_default(&mut self, enrich: Enrich) {
		if enrich != self.args.enrich_default {
			self.args.enrich_default = enrich;
			self.dirty = true;
		}
	}
	pub fn enrich_sysno(mut self, sysno: usize, enrich: Enrich) -> Self {
		let v = self.args.enrich_sysno.insert(sysno, enrich.clone());

		// Only set dirty flag if we actually made a change
		let dirty = if let Some(e) = v { e != enrich } else { true };

		self.dirty = dirty;
		self
	}
	builder_set_bool! { intercept_all_syscalls }

	#[cfg(feature = "syscalls")]
	builder_set_bool! { transform_syscalls }

	#[cfg(feature = "syscalls")]
	builder_set_bool! { only_notify_syscall_exit }

	#[cfg(feature = "syscalls")]
	builder_set_bool! { patch_ioctl_virtual }

	builder_set_bool! { handle_steps }
	builder_set_bool! { handle_exec }

	builder_push_val! { syscall_traced, sysno, usize }
	builder_push_val! { signal_traced, signal, i32 }

	builder_push_val! { registered, reg, RegEvent }
}

/// Object used by trace controllers to decide behaviour when tracing the
/// target. Construct with [ArgsBuilder]
#[derive(Serialize, Deserialize, Default, Debug, Clone)]
pub struct Args {
	intercept_all_syscalls: bool,
	// intercept_attached: bool,
	// intercept_clone: bool,
	syscall_traced: Vec<usize>,

	signal_traced: Vec<i32>,

	handle_steps: bool,

	handle_exec: bool,

	enrich_default: Enrich,
	enrich_sysno: HashMap<usize, Enrich>,

	/// If we should transform [Stop::SyscallEnter] and [Stop::SyscallExit] to
	/// [Event::Syscall]
	transform_syscalls: bool,

	/// Only notify when syscall has completed, when used with
	/// [Self::transform_syscalls], the caller still get all the argument.
	only_notify_syscall_exit: bool,

	patch_ioctl_virtual: bool,

	registered: Vec<RegEvent>,
	// attach_threads: bool,
}

impl Args {
	pub(crate) fn forward_master(mut self) -> Self {
		if (self.intercept_all_syscalls || !self.syscall_traced.is_empty())
			&& self.transform_syscalls
		{
			// To transform the syscalls, the proxy needs to receive enter as
			// well, so force it to be false
			self.only_notify_syscall_exit = false;
		}
		self
	}
	fn sanity_check(&self) -> Result<()> {
		if !self.intercept_all_syscalls
			&& !self.syscall_traced.is_empty()
			&& !self.transform_syscalls
		{
			Err(crate::Error::msg("for us to trace certain syscall we have to transform them to know the syscall number"))
		} else {
			Ok(())
		}
	}
	fn get_cont(&self) -> Wait {
		log::trace!("{:?}", self.syscall_traced);
		if self.intercept_all_syscalls || !self.syscall_traced.is_empty() {
			Wait::Syscall
		} else {
			Wait::Cont
		}
	}
	fn handles_syscall_enter(&self) -> bool {
		self.intercept_all_syscalls || !self.syscall_traced.is_empty()
	}
	fn handles_syscall_exit(&self) -> bool {
		self.intercept_all_syscalls || !self.syscall_traced.is_empty()
	}

	#[cfg(feature = "syscalls")]
	/// Returns true ifm we handle this syscall or all syscalls
	fn handles_syscall_sysno(&self, sysno: usize) -> bool {
		self.intercept_all_syscalls || self.syscall_traced.contains(&sysno)
	}
	#[cfg(feature = "syscalls")]
	fn enrich_syscall_sysno(&self, sysno: usize) -> Enrich {
		if let Some(enrich) = self.enrich_sysno.get(&sysno) {
			enrich.clone()
		} else {
			self.enrich_default.clone()
		}
	}
	fn handles_signal(&self, signal: i32) -> bool {
		self.signal_traced.contains(&signal)
	}

	pub(crate) fn handles_regevent(&self, reg: &RegEvent) -> bool {
		self.registered.contains(reg)
	}

	pub(crate) fn handles_stop(&self, stop: &Stop) -> Option<bool> {
		let r = match stop {
			Stop::SyscallEnter => Some(self.handles_syscall_enter()),
			Stop::SyscallExit => Some(self.handles_syscall_exit()),

			// We always return exit
			Stop::Exit { code: _ } => Some(true),
			Stop::Signal { signal, group: _ } => Some(self.handles_signal(*signal)),
			Stop::Clone { pid: _ } => Some(self.registered.contains(&RegEvent::Clone)),
			Stop::Attach => Some(self.registered.contains(&RegEvent::Attached)),
			Stop::Breakpoint { pc: _ } => None,
			Stop::Fork { newpid: _ } => Some(self.registered.contains(&RegEvent::Fork)),
			Stop::Step { pc: _ } => Some(self.handle_steps),
			Stop::Exec { old: _ } => Some(self.handle_exec),
			_ => {
				log::warn!("unimplemented signal {stop:?}");
				None
			}
		};
		log::debug!("handles stop {stop:?} => {r:?}");
		r
	}
}

#[derive(Default, Debug)]
pub(crate) struct ClientArgs {
	global: Args,
	threads: HashMap<Tid, Args>,
	/// All breakpoints this client is controlling
	bps: HashMap<TargetPtr, Breakpoint>,
}

impl ClientArgs {
	pub fn new(global: Args) -> Self {
		Self {
			global,
			threads: HashMap::new(),
			bps: HashMap::new(),
		}
	}
	pub fn get_cont(&self, tid: Tid) -> Wait {
		if let Some(args) = self.threads.get(&tid) {
			args.get_cont()
		} else {
			self.global.get_cont()
		}
	}
	pub fn detach_thread(&mut self, tid: Tid) {
		self.threads.insert(tid, Args::default());
	}
	pub fn clone_config(&self, tid: Option<Tid>) -> Option<Args> {
		if let Some(tid) = tid {
			self.threads.get(&tid).cloned()
		} else {
			Some(self.global.clone())
		}
	}
	pub fn insert_bp(&mut self, addr: TargetPtr) {
		// Breakpoints are always global
		let ins = Breakpoint::default();
		self.bps.insert(addr, ins);
	}
	fn handles_breakpint(&self, pc: TargetPtr) -> bool {
		self.bps.contains_key(&pc)
	}
	pub fn replace_config(&mut self, config: Args) {
		self.global = config;
	}
	pub fn replace_config_thread(&mut self, tid: Tid, config: Args) {
		self.threads.insert(tid, config);
	}

	#[cfg(feature = "syscalls")]
	bool_access! { intercept_all_syscalls }

	#[cfg(feature = "syscalls")]
	bool_access! { transform_syscalls }

	#[cfg(feature = "syscalls")]
	bool_access! { patch_ioctl_virtual }

	bool_access! { only_notify_syscall_exit }
	#[cfg(feature = "syscalls")]
	pub fn enrich_syscall_sysno(&self, tid: Tid, sysno: usize) -> Enrich {
		if let Some(args) = self.threads.get(&tid) {
			args.enrich_syscall_sysno(sysno)
		} else {
			self.global.enrich_syscall_sysno(sysno)
		}
	}

	#[cfg(feature = "syscalls")]
	pub fn handles_syscall_sysno(&self, tid: Tid, sysno: usize) -> bool {
		if let Some(args) = self.threads.get(&tid) {
			args.handles_syscall_sysno(sysno)
		} else {
			self.global.handles_syscall_sysno(sysno)
		}
	}
	pub fn handles_regevent(&self, reg: &RegEvent) -> bool {
		self.global.handles_regevent(reg)
	}

	pub fn handles_stop(&mut self, tid: Tid, stop: &Stop) -> bool {
		let handles = if let Some(args) = self.threads.get(&tid) {
			log::trace!("checking for tid {tid}");
			args.handles_stop(stop)
		} else {
			log::trace!("checking global");
			self.global.handles_stop(stop)
		};

		if let Some(ret) = handles {
			ret
		} else {
			// Breakpoint are always global, so we check this here instead
			if let Stop::Breakpoint { pc } = stop {
				self.handles_breakpint(*pc)
			} else {
				false
			}
		}
	}
}

#[cfg(test)]
mod test {
	use super::*;

	#[cfg(feature = "syscalls")]
	#[test]
	fn test_args1() {
		let args = ArgsBuilder::new()
			.handle_steps()
			.only_notify_syscall_exit()
			.finish()
			.unwrap();
		assert!(args.handle_steps);
		assert!(args.only_notify_syscall_exit);
		assert!(!args.handles_syscall_enter());
	}

	#[cfg(feature = "syscalls")]
	#[test]
	fn test_args2() {
		let args = ArgsBuilder::new()
			.intercept_all_syscalls()
			.finish()
			.unwrap();
		assert!(args.handles_syscall_enter());
		assert!(args.handles_syscall_exit());
		assert!(args.handles_syscall_sysno(1));
		assert!(args.intercept_all_syscalls);

		let cargs = ClientArgs::new(args);
		assert!(cargs.intercept_all_syscalls(1));
	}
}