Skip to contentSkip to conversation

python/sepolgen: allow any policy statement in ifndef

PATCH Superseded Vit Mojzis 2 participants 5 messages Jan 11, 2021 – Jan 27, 2021 SELinux security module

lore
Share & tools
lore

Vit Mojzis

author
#0 lore raw

PATCH 1/1 python/sepolgen: allow any policy statement in ifndef

"ifndef" statements can be used to conditionally define an interface, but this syntax is not recognised by sepolgen-ifgen.
Fix sepolgen-ifgen to allow any policy statement inside an "ifndef" statement.

Fixes:

        $ cat <<EOF > i.if
ifndef(`apache_manage_pid_files',`
        interface(`apache_manage_pid_files',`
                manage_files_pattern($1, httpd_var_run_t, httpd_var_run_t)
        ')
')

#sepolgen-ifgen --interface=i.if
i.if: Syntax error on line 2 interface [type=INTERFACE] i.if: Syntax error on line 4 ' [type=SQUOTE]

---
 python/sepolgen/src/sepolgen/refparser.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
python/sepolgen/src/sepolgen/refparser.py +1 -1
index 9f850990..3c69904a 100644
@@ -434,7 +434,7 @@ def p_ifelse(p):
 
 def p_ifdef(p):
     '''ifdef : IFDEF OPAREN TICK IDENTIFIER SQUOTE COMMA TICK interface_stmts SQUOTE CPAREN optional_semi
-             | IFNDEF OPAREN TICK IDENTIFIER SQUOTE COMMA TICK interface_stmts SQUOTE CPAREN optional_semi
+             | IFNDEF OPAREN TICK IDENTIFIER SQUOTE COMMA TICK statement SQUOTE CPAREN optional_semi
              | IFDEF OPAREN TICK IDENTIFIER SQUOTE COMMA TICK interface_stmts SQUOTE COMMA TICK interface_stmts SQUOTE CPAREN optional_semi
     '''
     x = refpolicy.IfDef(p[4])
Signature · 1 line
2.29.2
#1 lore raw
Quoted 27 lines · Vit Mojzis

"ifndef" statements can be used to conditionally define an interface, but this syntax is not recognised by sepolgen-ifgen.
Fix sepolgen-ifgen to allow any policy statement inside an "ifndef" statement.

Fixes:

        $ cat <<EOF > i.if
ifndef(`apache_manage_pid_files',`
        interface(`apache_manage_pid_files',`
                manage_files_pattern($1, httpd_var_run_t, httpd_var_run_t)
        ')
')

#sepolgen-ifgen --interface=i.if
i.if: Syntax error on line 2 interface [type=INTERFACE] i.if: Syntax error on line 4 ' [type=SQUOTE]

---
 python/sepolgen/src/sepolgen/refparser.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
python/sepolgen/src/sepolgen/refparser.py +0 -0
index 9f850990..3c69904a 100644

python/sepolgen/src/sepolgen/refparser.py def p_ifelse(p): · line 438

Quoted 6 lines
python/sepolgen/src/sepolgen/refparser.py +1 -1
@@ -434,7 +434,7 @@ def p_ifelse(p):
 
 def p_ifdef(p):
     '''ifdef : IFDEF OPAREN TICK IDENTIFIER SQUOTE COMMA TICK interface_stmts SQUOTE CPAREN optional_semi
-             | IFNDEF OPAREN TICK IDENTIFIER SQUOTE COMMA TICK interface_stmts SQUOTE CPAREN optional_semi
+             | IFNDEF OPAREN TICK IDENTIFIER SQUOTE COMMA TICK statement SQUOTE CPAREN optional_semi
              | IFDEF OPAREN TICK IDENTIFIER SQUOTE COMMA TICK interface_stmts SQUOTE COMMA TICK interface_stmts SQUOTE CPAREN optional_semi

IMHO we should be consistent and allow any statements also in ifdefs (i.e. make an equivalent change in the line before and after).

Quoted 5 lines
python/sepolgen/src/sepolgen/refparser.py +0 -1
     '''
     x = refpolicy.IfDef(p[4])
--

2.29.2

Signature · 3 lines
Ondrej Mosnacek
Software Engineer, Platform Security - SELinux kernel
Red Hat, Inc.
author
#2 lore raw

PATCH v2 1/1 python/sepolgen: allow any policy statement in if(n)def

"ifdef/ifndef" statements can be used to conditionally define an interface, but this syntax is not recognised by sepolgen-ifgen. Fix sepolgen-ifgen to allow any policy statement inside an "fidef/ifndef" statement.

Fixes:

        $ cat <<EOF > i.if
ifndef(`apache_manage_pid_files',`
        interface(`apache_manage_pid_files',`
                manage_files_pattern($1, httpd_var_run_t, httpd_var_run_t)
        ')
')

#sepolgen-ifgen --interface=i.if
i.if: Syntax error on line 2 interface [type=INTERFACE] i.if: Syntax error on line 4 ' [type=SQUOTE]

---
Thanks. Fixed. And I replaced "statement" with "statements" to allow multiple statements (as was possible with interface_stmts). I'm not all that sure about the last line since I didn't manage to find it's meaning (but I assume IF-THEN-ELSE).

 python/sepolgen/src/sepolgen/refparser.py | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
python/sepolgen/src/sepolgen/refparser.py +3 -3
index 9f850990..e611637f 100644
@@ -433,9 +433,9 @@ def p_ifelse(p):
 
 
 def p_ifdef(p):
-    '''ifdef : IFDEF OPAREN TICK IDENTIFIER SQUOTE COMMA TICK interface_stmts SQUOTE CPAREN optional_semi
-             | IFNDEF OPAREN TICK IDENTIFIER SQUOTE COMMA TICK interface_stmts SQUOTE CPAREN optional_semi
-             | IFDEF OPAREN TICK IDENTIFIER SQUOTE COMMA TICK interface_stmts SQUOTE COMMA TICK interface_stmts SQUOTE CPAREN optional_semi
+    '''ifdef : IFDEF OPAREN TICK IDENTIFIER SQUOTE COMMA TICK statements SQUOTE CPAREN optional_semi
+             | IFNDEF OPAREN TICK IDENTIFIER SQUOTE COMMA TICK statements SQUOTE CPAREN optional_semi
+             | IFDEF OPAREN TICK IDENTIFIER SQUOTE COMMA TICK statements SQUOTE COMMA TICK statements SQUOTE CPAREN optional_semi
     '''
     x = refpolicy.IfDef(p[4])
     if p[1] == 'ifdef':
Signature · 1 line
2.29.2
Acked-by on v2 1/1
#3 lore raw

Re: [PATCH v2] python/sepolgen: allow any policy statement in if(n)def

Quoted 17 lines · Vit Mojzis

"ifdef/ifndef" statements can be used to conditionally define an interface, but this syntax is not recognised by sepolgen-ifgen. Fix sepolgen-ifgen to allow any policy statement inside an "fidef/ifndef" statement.

Fixes:

        $ cat <<EOF > i.if
ifndef(`apache_manage_pid_files',`
        interface(`apache_manage_pid_files',`
                manage_files_pattern($1, httpd_var_run_t, httpd_var_run_t)
        ')
')

#sepolgen-ifgen --interface=i.if
i.if: Syntax error on line 2 interface [type=INTERFACE] i.if: Syntax error on line 4 ' [type=SQUOTE]

Quoted 6 lines

---
Thanks. Fixed. And I replaced "statement" with "statements" to allow multiple statements (as was possible with interface_stmts). I'm not all that sure about the last line since I didn't manage to find it's meaning (but I assume IF-THEN-ELSE).

Yes, the third argument is basically an optional 'else' branch:
https://www.gnu.org/software/m4/manual/m4-1.4.15/html_node/Ifdef.html

Quoted 22 lines
 python/sepolgen/src/sepolgen/refparser.py | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
python/sepolgen/src/sepolgen/refparser.py +3 -4
index 9f850990..e611637f 100644
@@ -433,9 +433,9 @@ def p_ifelse(p):
 
 
 def p_ifdef(p):
-    '''ifdef : IFDEF OPAREN TICK IDENTIFIER SQUOTE COMMA TICK interface_stmts SQUOTE CPAREN optional_semi
-             | IFNDEF OPAREN TICK IDENTIFIER SQUOTE COMMA TICK interface_stmts SQUOTE CPAREN optional_semi
-             | IFDEF OPAREN TICK IDENTIFIER SQUOTE COMMA TICK interface_stmts SQUOTE COMMA TICK interface_stmts SQUOTE CPAREN optional_semi
+    '''ifdef : IFDEF OPAREN TICK IDENTIFIER SQUOTE COMMA TICK statements SQUOTE CPAREN optional_semi
+             | IFNDEF OPAREN TICK IDENTIFIER SQUOTE COMMA TICK statements SQUOTE CPAREN optional_semi
+             | IFDEF OPAREN TICK IDENTIFIER SQUOTE COMMA TICK statements SQUOTE COMMA TICK statements SQUOTE CPAREN optional_semi
     '''
     x = refpolicy.IfDef(p[4])
     if p[1] == 'ifdef':
--
Quoted 2 lines

2.29.2

--
Ondrej Mosnacek
Software Engineer, Platform Security - SELinux kernel Red Hat, Inc.

#4 lore raw

Re: [PATCH v2] python/sepolgen: allow any policy statement in if(n)def

Quoted 39 lines · Ondrej Mosnacek
Quoted 17 lines · Vit Mojzis

"ifdef/ifndef" statements can be used to conditionally define an interface, but this syntax is not recognised by sepolgen-ifgen. Fix sepolgen-ifgen to allow any policy statement inside an "fidef/ifndef" statement.

Fixes:

        $ cat <<EOF > i.if
ifndef(`apache_manage_pid_files',`
        interface(`apache_manage_pid_files',`
                manage_files_pattern($1, httpd_var_run_t, httpd_var_run_t)
        ')
')

#sepolgen-ifgen --interface=i.if
i.if: Syntax error on line 2 interface [type=INTERFACE] i.if: Syntax error on line 4 ' [type=SQUOTE]

Quoted 6 lines

---
Thanks. Fixed. And I replaced "statement" with "statements" to allow multiple statements (as was possible with interface_stmts). I'm not all that sure about the last line since I didn't manage to find it's meaning (but I assume IF-THEN-ELSE).

Yes, the third argument is basically an optional 'else' branch:
https://www.gnu.org/software/m4/manual/m4-1.4.15/html_node/Ifdef.html

Quoted 11 lines
 python/sepolgen/src/sepolgen/refparser.py | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
python/sepolgen/src/sepolgen/refparser.py +0 -0
index 9f850990..e611637f 100644
@@ -433,9 +433,9 @@ def p_ifelse(p):
 
 
Quoted 15 lines
Quoted 11 lines
python/sepolgen/src/sepolgen/refparser.py +3 -4
 def p_ifdef(p):
-    '''ifdef : IFDEF OPAREN TICK IDENTIFIER SQUOTE COMMA TICK interface_stmts SQUOTE CPAREN optional_semi
-             | IFNDEF OPAREN TICK IDENTIFIER SQUOTE COMMA TICK interface_stmts SQUOTE CPAREN optional_semi
-             | IFDEF OPAREN TICK IDENTIFIER SQUOTE COMMA TICK interface_stmts SQUOTE COMMA TICK interface_stmts SQUOTE CPAREN optional_semi
+    '''ifdef : IFDEF OPAREN TICK IDENTIFIER SQUOTE COMMA TICK statements SQUOTE CPAREN optional_semi
+             | IFNDEF OPAREN TICK IDENTIFIER SQUOTE COMMA TICK statements SQUOTE CPAREN optional_semi
+             | IFDEF OPAREN TICK IDENTIFIER SQUOTE COMMA TICK statements SQUOTE COMMA TICK statements SQUOTE CPAREN optional_semi
     '''
     x = refpolicy.IfDef(p[4])
     if p[1] == 'ifdef':
--
Quoted 2 lines

2.29.2

Merged:
https://github.com/SELinuxProject/selinux/commit/e12f71e82f523513c249dbe5a2e35c1916370992

Signature · 3 lines
Ondrej Mosnacek
Software Engineer, Platform Security - SELinux kernel
Red Hat, Inc.