1
1
use anyhow:: { anyhow, Error } ;
2
- use evidence_api:: { api:: EvidenceApi , api_data:: ExtraArgs , tcg} ;
3
2
use cctrusted_vm:: sdk:: API ;
3
+ use evidence_api:: { api:: EvidenceApi , api_data:: ExtraArgs , tcg} ;
4
4
use log:: info;
5
5
use std:: cmp:: Ordering ;
6
6
use std:: collections:: HashMap ;
7
+ use std:: fs:: read_to_string;
7
8
8
9
use crate :: {
9
10
cima_pb:: { TcgDigest , TcgEventlog } ,
@@ -12,6 +13,8 @@ use crate::{
12
13
policy:: PolicyConfig ,
13
14
} ;
14
15
16
+ pub const IMA_PATTERN : & str = "ima_template=ima-cgpath" ;
17
+
15
18
pub enum IMR {
16
19
FIRMWARE = 0 ,
17
20
KERNEL = 1 ,
@@ -23,6 +26,7 @@ pub struct Agent {
23
26
measurement : Option < Measurement > ,
24
27
containers : HashMap < String , Container > ,
25
28
event_logs : Vec < TcgEventlog > ,
29
+ ima_enabled : bool ,
26
30
}
27
31
28
32
impl Default for Agent {
@@ -37,20 +41,27 @@ impl Agent {
37
41
measurement : None ,
38
42
containers : HashMap :: new ( ) ,
39
43
event_logs : vec ! [ ] ,
44
+ ima_enabled : false ,
40
45
}
41
46
}
42
47
43
48
pub fn init ( & mut self , policy : PolicyConfig ) -> Result < ( ) , Error > {
44
- // Measure the system when Agent initialization
45
- self . measurement = Some ( Measurement :: new ( policy) ) ;
46
- match self
47
- . measurement
48
- . as_mut ( )
49
- . expect ( "The measurement was not initialized." )
50
- . measure ( )
51
- {
52
- Ok ( _) => info ! ( "The system has been measured as the policy defined." ) ,
53
- Err ( e) => return Err ( e) ,
49
+ let cmdline = read_to_string ( "/proc/cmdline" ) . expect ( "Failed to read /proc/cmdline." ) ;
50
+ if !cmdline. contains ( IMA_PATTERN ) {
51
+ self . ima_enabled = false ;
52
+ } else {
53
+ self . ima_enabled = true ;
54
+ // Measure the system when Agent initialization
55
+ self . measurement = Some ( Measurement :: new ( policy) ) ;
56
+ match self
57
+ . measurement
58
+ . as_mut ( )
59
+ . expect ( "The measurement was not initialized." )
60
+ . measure ( )
61
+ {
62
+ Ok ( _) => info ! ( "The system has been measured as the policy defined." ) ,
63
+ Err ( e) => return Err ( e) ,
64
+ }
54
65
}
55
66
56
67
self . fetch_all_event_logs ( )
@@ -198,26 +209,30 @@ impl Agent {
198
209
let _ = self . fetch_all_event_logs ( ) ;
199
210
let mut event_logs = vec ! [ ] ;
200
211
201
- let measurement = match self . measurement . as_mut ( ) {
202
- Some ( v) => v,
203
- None => return Err ( anyhow ! ( "The measurement was not initialized." ) ) ,
204
- } ;
212
+ if self . ima_enabled {
213
+ let measurement = match self . measurement . as_mut ( ) {
214
+ Some ( v) => v,
215
+ None => return Err ( anyhow ! ( "The measurement was not initialized." ) ) ,
216
+ } ;
205
217
206
- if measurement. container_isolated ( ) {
207
- if !self . containers . contains_key ( & container_id) {
208
- return Err ( anyhow ! ( "Container cannot be found." ) ) ;
209
- }
218
+ if measurement. container_isolated ( ) {
219
+ if !self . containers . contains_key ( & container_id) {
220
+ return Err ( anyhow ! ( "Container cannot be found." ) ) ;
221
+ }
210
222
211
- for event_log in & self . event_logs {
212
- if event_log. imr_index == IMR :: FIRMWARE as u32
213
- || event_log. imr_index == IMR :: KERNEL as u32
214
- {
215
- event_logs. push ( event_log. clone ( ) ) ;
223
+ for event_log in & self . event_logs {
224
+ if event_log. imr_index == IMR :: FIRMWARE as u32
225
+ || event_log. imr_index == IMR :: KERNEL as u32
226
+ {
227
+ event_logs. push ( event_log. clone ( ) ) ;
228
+ }
216
229
}
217
- }
218
230
219
- let container = & self . containers [ & container_id] ;
220
- event_logs. extend ( container. event_logs ( ) . clone ( ) ) ;
231
+ let container = & self . containers [ & container_id] ;
232
+ event_logs. extend ( container. event_logs ( ) . clone ( ) ) ;
233
+ } else {
234
+ event_logs. extend ( self . event_logs . to_vec ( ) ) ;
235
+ }
221
236
} else {
222
237
event_logs. extend ( self . event_logs . to_vec ( ) ) ;
223
238
}
@@ -262,23 +277,27 @@ impl Agent {
262
277
) -> Result < ( Vec < u8 > , i32 ) , Error > {
263
278
let _ = self . fetch_all_event_logs ( ) ;
264
279
265
- let measurement = match self . measurement . as_mut ( ) {
266
- Some ( v) => v,
267
- None => return Err ( anyhow ! ( "The measurement was not initialized." ) ) ,
268
- } ;
280
+ let new_nonce = if self . ima_enabled {
281
+ let measurement = match self . measurement . as_mut ( ) {
282
+ Some ( v) => v,
283
+ None => return Err ( anyhow ! ( "The measurement was not initialized." ) ) ,
284
+ } ;
269
285
270
- let new_nonce = if measurement. container_isolated ( ) {
271
- if !self . containers . contains_key ( & container_id) {
272
- return Err ( anyhow ! ( "Container cannot be found." ) ) ;
273
- }
286
+ if measurement. container_isolated ( ) {
287
+ if !self . containers . contains_key ( & container_id) {
288
+ return Err ( anyhow ! ( "Container cannot be found." ) ) ;
289
+ }
274
290
275
- let container = & self . containers [ & container_id] ;
276
- match nonce {
277
- Some ( v) => match base64:: decode ( v) {
278
- Ok ( v) => Some ( base64:: encode ( [ container. imr ( ) . hash . to_vec ( ) , v] . concat ( ) ) ) ,
279
- Err ( e) => return Err ( anyhow ! ( "nonce is not base64 encoded: {:?}" , e) ) ,
280
- } ,
281
- None => None ,
291
+ let container = & self . containers [ & container_id] ;
292
+ match nonce {
293
+ Some ( v) => match base64:: decode ( v) {
294
+ Ok ( v) => Some ( base64:: encode ( [ container. imr ( ) . hash . to_vec ( ) , v] . concat ( ) ) ) ,
295
+ Err ( e) => return Err ( anyhow ! ( "nonce is not base64 encoded: {:?}" , e) ) ,
296
+ } ,
297
+ None => None ,
298
+ }
299
+ } else {
300
+ nonce. clone ( )
282
301
}
283
302
} else {
284
303
nonce. clone ( )
@@ -300,28 +319,32 @@ impl Agent {
300
319
) -> Result < TcgDigest , Error > {
301
320
let _ = self . fetch_all_event_logs ( ) ;
302
321
303
- let measurement = match self . measurement . as_mut ( ) {
304
- Some ( v) => v,
305
- None => return Err ( anyhow ! ( "The measurement was not initialized." ) ) ,
306
- } ;
322
+ if self . ima_enabled {
323
+ let measurement = match self . measurement . as_mut ( ) {
324
+ Some ( v) => v,
325
+ None => return Err ( anyhow ! ( "The measurement was not initialized." ) ) ,
326
+ } ;
307
327
308
- if measurement. container_isolated ( ) {
309
- if !self . containers . contains_key ( & container_id) {
310
- return Err ( anyhow ! ( "Container cannot be found." ) ) ;
311
- }
328
+ if measurement. container_isolated ( ) {
329
+ if !self . containers . contains_key ( & container_id) {
330
+ return Err ( anyhow ! ( "Container cannot be found." ) ) ;
331
+ }
312
332
313
- if index == IMR :: SYSTEM as u32 {
314
- return Err ( anyhow ! ( "Cannot access IMR according to the policy." ) ) ;
315
- }
333
+ if index == IMR :: SYSTEM as u32 {
334
+ return Err ( anyhow ! ( "Cannot access IMR according to the policy." ) ) ;
335
+ }
316
336
317
- if index == IMR :: CONTAINER as u32 {
318
- let container = match self . containers . get_mut ( & container_id) {
319
- Some ( v) => v,
320
- None => {
321
- return Err ( anyhow ! ( "The container is on the list but fails to get it." ) )
322
- }
323
- } ;
324
- return Ok ( container. imr ( ) . clone ( ) ) ;
337
+ if index == IMR :: CONTAINER as u32 {
338
+ let container = match self . containers . get_mut ( & container_id) {
339
+ Some ( v) => v,
340
+ None => {
341
+ return Err ( anyhow ! (
342
+ "The container is on the list but fails to get it."
343
+ ) )
344
+ }
345
+ } ;
346
+ return Ok ( container. imr ( ) . clone ( ) ) ;
347
+ }
325
348
}
326
349
}
327
350
0 commit comments