@@ -176,18 +176,22 @@ def _get_permission_confusion_count(
176
176
return ret .fetchone ()[0 ]
177
177
178
178
def get_permission_confusion (
179
- self , case : str , context : str , eval_case : str
179
+ self , case : str , contexts : Iterable [ str ] , eval_case : str
180
180
) -> tuple [int , int , int , int ]:
181
181
case_id = self .get_case_id (case )
182
- subject_cid = self .get_context_id (context )
182
+ subject_cids = [ self .get_context_id (context ) for context in contexts ]
183
183
eval_case_id = self .get_eval_case (eval_case )
184
184
results = ((1 , 1 ), (0 , 0 ), (0 , 1 ), (1 , 0 ))
185
- return [
186
- self ._get_permission_confusion_count (
187
- case_id , subject_cid , eval_case_id , * result
188
- )
189
- for result in results
185
+ ret = [
186
+ [
187
+ self ._get_permission_confusion_count (
188
+ case_id , subject_cid , eval_case_id , * result
189
+ )
190
+ for result in results
191
+ ]
192
+ for subject_cid in subject_cids
190
193
]
194
+ return tuple (sum (x ) for x in zip (* ret ))
191
195
192
196
193
197
class DatabaseWriter (DatabaseCommon ):
@@ -311,24 +315,18 @@ def _insert_selinux_access(
311
315
print (
312
316
f'{ subject_context } =>{ context } { path } ({ _class } :{ perm } )={ result } '
313
317
)
314
- self .cur .execute (
315
- 'INSERT INTO accesses VALUES(?, ?, ?)' ,
316
- (
317
- case_id ,
318
- subject_cid ,
319
- rowid ,
320
- ),
321
- )
322
318
323
- access_id = self .cur .lastrowid
319
+ access_id = self .insert_or_select_access (
320
+ case_id , subject_cid , path_rowid
321
+ )
324
322
325
323
for perm_id , result in zip (perms_id , results ):
326
324
self .insert_ref_result (access_id , perm_id , result )
327
325
328
326
def insert_selinux_accesses (
329
327
self ,
330
328
case_name : str ,
331
- subject_context : str ,
329
+ subject_contexts : Iterable [ str ] ,
332
330
object_types : Iterable [str ],
333
331
verbose : bool = False ,
334
332
):
@@ -350,11 +348,15 @@ def insert_selinux_accesses(
350
348
(case_name ,),
351
349
)
352
350
case_id = self .get_case_id (case_name )
353
- self .cur .execute (
354
- 'INSERT INTO contexts VALUES(?) ON CONFLICT DO NOTHING' ,
355
- (subject_context ,),
356
- )
357
- subject_cid = self .get_context_id (subject_context )
351
+ for subject_context in subject_contexts :
352
+ self .cur .execute (
353
+ 'INSERT INTO contexts VALUES(?) ON CONFLICT DO NOTHING' ,
354
+ (subject_context ,),
355
+ )
356
+ subject_cids = [
357
+ self .get_context_id (subject_context )
358
+ for subject_context in subject_contexts
359
+ ]
358
360
files = self .get_paths_by_selinux_type (object_types )
359
361
perms = ('read' , 'write' )
360
362
perms_id = self .get_operations_id (perms )
@@ -387,25 +389,17 @@ def insert_selinux_accesses(
387
389
print (
388
390
f'{ subject_context } =>{ context } { path } ({ _class } :{ perm } )={ result } '
389
391
)
390
- self .cur .execute (
391
- 'INSERT INTO accesses VALUES(?, ?, ?)' ,
392
- (
393
- case_id ,
394
- subject_cid ,
395
- rowid ,
396
- ),
397
- )
398
-
399
- access_id = self .cur .lastrowid
392
+ for subject_cid in subject_cids :
393
+ access_id = self .insert_or_select_access (
394
+ case_id , subject_cid , path_rowid
395
+ )
400
396
401
- for perm_id , result in zip (perms_id , results ):
402
- self .insert_ref_result (access_id , perm_id , result )
397
+ for perm_id , result in zip (perms_id , results ):
398
+ self .insert_ref_result (access_id , perm_id , result )
403
399
404
400
def fill_missing_selinux_accesses (
405
401
self ,
406
402
case_name : str ,
407
- subject_context : str ,
408
- object_types : Iterable [str ],
409
403
verbose : bool = False ,
410
404
):
411
405
"""Fill missing accesses for SELinux in the database.
@@ -415,14 +409,11 @@ def fill_missing_selinux_accesses(
415
409
416
410
:param case_name: Name of the service that is examined. This will be
417
411
used as a unique value in the database.
418
- :param subject_context: SELinux context of the subject.
419
- :param object_types:SELinux types that will be searched in the database
420
- and found files will be examined for read and write permissions from the
421
- subject.
422
412
:param verbose: Turns on verbose output.
423
413
"""
424
414
perms = ('read' , 'write' )
425
415
perms_id = self .get_operations_id (perms )
416
+ case_id = self .get_case_id (case_name )
426
417
427
418
res = self .cur .execute (
428
419
"""WITH RECURSIVE child AS
@@ -447,7 +438,7 @@ def fill_missing_selinux_accesses(
447
438
JOIN fs ON node_rowid = fs.rowid
448
439
LEFT JOIN results ON accesses.ROWID = results.access_id
449
440
LEFT JOIN operations ON results.operation_id = operations.rowid
450
- WHERE case_id = 1
441
+ WHERE case_id = ?
451
442
AND reference_result IS NULL
452
443
UNION ALL SELECT access_rowid,
453
444
node_rowid,
@@ -480,7 +471,8 @@ def fill_missing_selinux_accesses(
480
471
reference_result
481
472
FROM child
482
473
WHERE rowid = 1
483
- """
474
+ """ ,
475
+ (case_id ,),
484
476
)
485
477
accesses = res .fetchall ()
486
478
for (
@@ -502,7 +494,7 @@ def fill_missing_selinux_accesses(
502
494
_class = 'dir' if is_dir else 'file'
503
495
504
496
if operation_id is None :
505
- # Computer access permissions for all operations
497
+ # Compute access permissions for all operations
506
498
results = [
507
499
selinux_check_access (
508
500
subject_context , selinux_context , _class , perm
0 commit comments