xref: /aosp_15_r20/system/sepolicy/tests/searchpolicy.py (revision e4a36f4174b17bbab9dc043f4a65dc8d87377290)
1*e4a36f41SAndroid Build Coastguard Worker#!/usr/bin/env python3
2*e4a36f41SAndroid Build Coastguard Worker#
3*e4a36f41SAndroid Build Coastguard Worker# Copyright 2021 The Android Open Source Project
4*e4a36f41SAndroid Build Coastguard Worker#
5*e4a36f41SAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License");
6*e4a36f41SAndroid Build Coastguard Worker# you may not use this file except in compliance with the License.
7*e4a36f41SAndroid Build Coastguard Worker# You may obtain a copy of the License at
8*e4a36f41SAndroid Build Coastguard Worker#
9*e4a36f41SAndroid Build Coastguard Worker#     http://www.apache.org/licenses/LICENSE-2.0
10*e4a36f41SAndroid Build Coastguard Worker#
11*e4a36f41SAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software
12*e4a36f41SAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS,
13*e4a36f41SAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14*e4a36f41SAndroid Build Coastguard Worker# See the License for the specific language governing permissions and
15*e4a36f41SAndroid Build Coastguard Worker# limitations under the License.
16*e4a36f41SAndroid Build Coastguard Worker
17*e4a36f41SAndroid Build Coastguard Workerimport argparse
18*e4a36f41SAndroid Build Coastguard Workerimport policy
19*e4a36f41SAndroid Build Coastguard Worker
20*e4a36f41SAndroid Build Coastguard Workerparser = argparse.ArgumentParser(
21*e4a36f41SAndroid Build Coastguard Worker    description="SELinux policy rule search tool. Intended to have a similar "
22*e4a36f41SAndroid Build Coastguard Worker        + "API as sesearch, but simplified to use only code availabe in AOSP")
23*e4a36f41SAndroid Build Coastguard Workerparser.add_argument("policy", help="Path to the SELinux policy to search.", nargs="?")
24*e4a36f41SAndroid Build Coastguard Workerparser.add_argument("--libpath", dest="libpath", help="Path to the libsepolwrap.so", nargs="?")
25*e4a36f41SAndroid Build Coastguard Workertertypes = parser.add_argument_group("TE Rule Types")
26*e4a36f41SAndroid Build Coastguard Workertertypes.add_argument("--allow", action="append_const",
27*e4a36f41SAndroid Build Coastguard Worker                    const="allow", dest="tertypes",
28*e4a36f41SAndroid Build Coastguard Worker                    help="Search allow rules.")
29*e4a36f41SAndroid Build Coastguard Workerexpr = parser.add_argument_group("Expressions")
30*e4a36f41SAndroid Build Coastguard Workerexpr.add_argument("-s", "--source",
31*e4a36f41SAndroid Build Coastguard Worker                  help="Source type/role of the TE/RBAC rule.")
32*e4a36f41SAndroid Build Coastguard Workerexpr.add_argument("-t", "--target",
33*e4a36f41SAndroid Build Coastguard Worker                  help="Target type/role of the TE/RBAC rule.")
34*e4a36f41SAndroid Build Coastguard Workerexpr.add_argument("-c", "--class", dest="tclass",
35*e4a36f41SAndroid Build Coastguard Worker                  help="Comma separated list of object classes")
36*e4a36f41SAndroid Build Coastguard Workerexpr.add_argument("-p", "--perms", metavar="PERMS",
37*e4a36f41SAndroid Build Coastguard Worker                  help="Comma separated list of permissions.")
38*e4a36f41SAndroid Build Coastguard Worker
39*e4a36f41SAndroid Build Coastguard Workerargs = parser.parse_args()
40*e4a36f41SAndroid Build Coastguard Worker
41*e4a36f41SAndroid Build Coastguard Workerif not args.tertypes:
42*e4a36f41SAndroid Build Coastguard Worker    parser.error("Must specify \"--allow\"")
43*e4a36f41SAndroid Build Coastguard Worker
44*e4a36f41SAndroid Build Coastguard Workerif not args.policy:
45*e4a36f41SAndroid Build Coastguard Worker    parser.error("Must include path to policy")
46*e4a36f41SAndroid Build Coastguard Workerif not args.libpath:
47*e4a36f41SAndroid Build Coastguard Worker    parser.error("Must include path to libsepolwrap library")
48*e4a36f41SAndroid Build Coastguard Worker
49*e4a36f41SAndroid Build Coastguard Workerif not (args.source or args.target or args.tclass or args.perms):
50*e4a36f41SAndroid Build Coastguard Worker    parser.error("Must something to filter on, e.g. --source, --target, etc.")
51*e4a36f41SAndroid Build Coastguard Worker
52*e4a36f41SAndroid Build Coastguard Workerpol = policy.Policy(args.policy, None, args.libpath)
53*e4a36f41SAndroid Build Coastguard Worker
54*e4a36f41SAndroid Build Coastguard Workerif args.source:
55*e4a36f41SAndroid Build Coastguard Worker    scontext = {args.source}
56*e4a36f41SAndroid Build Coastguard Workerelse:
57*e4a36f41SAndroid Build Coastguard Worker    scontext = set()
58*e4a36f41SAndroid Build Coastguard Workerif args.target:
59*e4a36f41SAndroid Build Coastguard Worker    tcontext = {args.target}
60*e4a36f41SAndroid Build Coastguard Workerelse:
61*e4a36f41SAndroid Build Coastguard Worker    tcontext = set()
62*e4a36f41SAndroid Build Coastguard Workerif args.tclass:
63*e4a36f41SAndroid Build Coastguard Worker    tclass = set(args.tclass.split(","))
64*e4a36f41SAndroid Build Coastguard Workerelse:
65*e4a36f41SAndroid Build Coastguard Worker    tclass = set()
66*e4a36f41SAndroid Build Coastguard Workerif args.perms:
67*e4a36f41SAndroid Build Coastguard Worker    perms = set(args.perms.split(","))
68*e4a36f41SAndroid Build Coastguard Workerelse:
69*e4a36f41SAndroid Build Coastguard Worker    perms = set()
70*e4a36f41SAndroid Build Coastguard Worker
71*e4a36f41SAndroid Build Coastguard WorkerTERules = pol.QueryTERule(scontext=scontext,
72*e4a36f41SAndroid Build Coastguard Worker                       tcontext=tcontext,
73*e4a36f41SAndroid Build Coastguard Worker                       tclass=tclass,
74*e4a36f41SAndroid Build Coastguard Worker                       perms=perms)
75*e4a36f41SAndroid Build Coastguard Worker
76*e4a36f41SAndroid Build Coastguard Worker# format rules for printing
77*e4a36f41SAndroid Build Coastguard Workerrules = []
78*e4a36f41SAndroid Build Coastguard Workerfor r in TERules:
79*e4a36f41SAndroid Build Coastguard Worker    if len(r.perms) > 1:
80*e4a36f41SAndroid Build Coastguard Worker        rules.append("allow " + r.sctx + " " + r.tctx + ":" + r.tclass + " { " +
81*e4a36f41SAndroid Build Coastguard Worker                " ".join(sorted(r.perms)) + " };")
82*e4a36f41SAndroid Build Coastguard Worker    else:
83*e4a36f41SAndroid Build Coastguard Worker        rules.append("allow " + r.sctx + " " + r.tctx + ":" + r.tclass + " " +
84*e4a36f41SAndroid Build Coastguard Worker                " ".join(sorted(r.perms)) + ";")
85*e4a36f41SAndroid Build Coastguard Worker
86*e4a36f41SAndroid Build Coastguard Workerfor r in sorted(rules):
87*e4a36f41SAndroid Build Coastguard Worker    print(r)
88