Skip to contentSkip to conversation

checkmodule: allow building modules of a specific version

PATCH In review Gary Tierney 3 participants 9 messages Apr 17, 2019 – Apr 19, 2019 SELinux security module

lore
Share & tools
lore
#0 lore raw

cover checkmodule: allow building modules of a specific version

These changes come from a report by a user on the Freenode IRC channel that they were unable to build policies for a machine that has an older version of libsepol installed.

A new -c option that mirrors checkpolicy's own has been added to checkmodule, and the output of a simple test is shown below:

$ cat > test.te <<EOF
module test 1.0;
require {
    type domain;
    type file_type;
    class file { read write };
}

allow domain file_type : file { read write };
EOF

$ obj/usr/bin/checkmodule -m -M -c 10 -o test.mod test.te
$ checkpolicy/test/dismod test.mod
Reading policy...
... snip ...
Binary policy module file loaded.
Module name: test
Module version: 1.0
Policy version: 10

Worthy of note, however, is that these policy version numbers differ from those used by the kernel policy format.

Gary Tierney (2):
checkmodule: add support for specifying module policy version
dismod: print policy version of loaded modules

 checkpolicy/checkmodule.8 |  5 ++++-
 checkpolicy/checkmodule.c | 29 +++++++++++++++++++++++++++--
 checkpolicy/test/dismod.c |  4 ++--
 3 files changed, 33 insertions(+), 5 deletions(-)
Signature · 1 line
2.17.2
#1 lore raw

PATCH 1/2 checkmodule: add support for specifying module policy version

Currently checkpolicy can produce binary policies for earlier policy versions to provide support for building policies on one machine and loading/analyzing them on another machine with an earlier version of the kernel or libsepol, respectively. However, checkmodule was lacking this capability.

This commit adds an identical -c flag that can be passed to checkmodule that will build a modular policy file of the specified version.

---
 checkpolicy/checkmodule.8 |  5 ++++-
 checkpolicy/checkmodule.c | 29 +++++++++++++++++++++++++++--
 2 files changed, 31 insertions(+), 3 deletions(-)
checkpolicy/checkmodule.8 +4 -1
index cf76591c24d0..e55582f30ec0 100644
@@ -38,7 +38,7 @@ Generate a non-base policy module.
 Enable the MLS/MCS support when checking and compiling the policy module.
 .TP
 .B \-V,\-\-version
- Show policy versions created by this program.  Note that you cannot currently build older versions.
+Show policy versions created by this program.
 .TP
 .B \-o,\-\-output filename
 Write a binary policy module file to the specified filename.
@@ -47,6 +47,9 @@ and will not generate a binary module at all.
 .TP
 .B \-U,\-\-handle-unknown <action>
 Specify how the kernel should handle unknown classes or permissions (deny, allow or reject).
+.TP
+.B \-c policyvers
+Specify the policy version, defaults to the latest.
 
 .SH EXAMPLE
 .nf
checkpolicy/checkmodule.c +27 -2
index 8edc1f8c7bbd..3bb9e5a4a6b3 100644
@@ -142,6 +142,8 @@ static __attribute__((__noreturn__)) void usage(const char *progname)
 	printf("  -m         build a policy module instead of a base module\n");
 	printf("  -M         enable MLS policy\n");
 	printf("  -o FILE    write module to FILE (else just check syntax)\n");
+	printf("  -c VERSION build a policy module targeting a modular policy version (%d-%d)\n",
+	       MOD_POLICYDB_VERSION_MIN, MOD_POLICYDB_VERSION_MAX);
 	exit(1);
 }
 
@@ -163,7 +165,7 @@ int main(int argc, char **argv)
 		{NULL, 0, NULL, 0}
 	};
 
-	while ((ch = getopt_long(argc, argv, "ho:bVU:mMC", long_options, NULL)) != -1) {
+	while ((ch = getopt_long(argc, argv, "ho:bVU:mMCc:", long_options, NULL)) != -1) {
 		switch (ch) {
 		case 'h':
 			usage(argv[0]);
@@ -194,7 +196,6 @@ int main(int argc, char **argv)
 			usage(argv[0]);
 		case 'm':
 			policy_type = POLICY_MOD;
-			policyvers = MOD_POLICYDB_VERSION_MAX;
 			break;
 		case 'M':
 			mlspol = 1;
@@ -202,6 +203,30 @@ int main(int argc, char **argv)
 		case 'C':
 			cil = 1;
 			break;
+		case 'c': {
+			long int n;
+			errno = 0;
+			n = strtol(optarg, NULL, 10);
+
+			if (errno) {
+				fprintf(stderr,
+					"Invalid policyvers specified: %s\n",
+					optarg);
+				usage(argv[0]);
+			}
+
+			if (n < MOD_POLICYDB_VERSION_MIN
+			    || n > MOD_POLICYDB_VERSION_MAX) {
+				fprintf(stderr,
+					"policyvers value %ld not in range %d-%d\n",
+					n, MOD_POLICYDB_VERSION_MIN,
+					MOD_POLICYDB_VERSION_MAX);
+				usage(argv[0]);
+			}
+
+			policyvers = n;
+			break;
+		}
 		default:
 			usage(argv[0]);
 		}
Signature · 1 line
2.17.2
#2 lore raw

Re: [PATCH 1/2] checkmodule: add support for specifying module policy version

Quoted 69 lines · Gary Tierney

Currently checkpolicy can produce binary policies for earlier policy versions to provide support for building policies on one machine and loading/analyzing them on another machine with an earlier version of the kernel or libsepol, respectively. However, checkmodule was lacking this capability.

This commit adds an identical -c flag that can be passed to checkmodule that will build a modular policy file of the specified version.

---
 checkpolicy/checkmodule.8 |  5 ++++-
 checkpolicy/checkmodule.c | 29 +++++++++++++++++++++++++++--
 2 files changed, 31 insertions(+), 3 deletions(-)
checkpolicy/checkmodule.8 +4 -1
index cf76591c24d0..e55582f30ec0 100644
@@ -38,7 +38,7 @@ Generate a non-base policy module.
 Enable the MLS/MCS support when checking and compiling the policy module.
 .TP
 .B \-V,\-\-version
- Show policy versions created by this program.  Note that you cannot currently build older versions.
+Show policy versions created by this program.
 .TP
 .B \-o,\-\-output filename
 Write a binary policy module file to the specified filename.
@@ -47,6 +47,9 @@ and will not generate a binary module at all.
 .TP
 .B \-U,\-\-handle-unknown <action>
 Specify how the kernel should handle unknown classes or permissions (deny, allow or reject).
+.TP
+.B \-c policyvers
+Specify the policy version, defaults to the latest.
 
 .SH EXAMPLE
 .nf
checkpolicy/checkmodule.c +3 -2
index 8edc1f8c7bbd..3bb9e5a4a6b3 100644
@@ -142,6 +142,8 @@ static __attribute__((__noreturn__)) void usage(const char *progname)
        printf("  -m         build a policy module instead of a base module\n");
        printf("  -M         enable MLS policy\n");
        printf("  -o FILE    write module to FILE (else just check syntax)\n");
+       printf("  -c VERSION build a policy module targeting a modular policy version (%d-%d)\n",
+              MOD_POLICYDB_VERSION_MIN, MOD_POLICYDB_VERSION_MAX);
        exit(1);
 }
 
@@ -163,7 +165,7 @@ int main(int argc, char **argv)
                {NULL, 0, NULL, 0}
        };
 
-       while ((ch = getopt_long(argc, argv, "ho:bVU:mMC", long_options, NULL)) != -1) {
+       while ((ch = getopt_long(argc, argv, "ho:bVU:mMCc:", long_options, NULL)) != -1) {
                switch (ch) {
                case 'h':
                        usage(argv[0]);
@@ -194,7 +196,6 @@ int main(int argc, char **argv)
                        usage(argv[0]);
                case 'm':
                        policy_type = POLICY_MOD;
-                       policyvers = MOD_POLICYDB_VERSION_MAX;
                        break;
                case 'M':
                        mlspol = 1;

checkpolicy/checkmodule.c int main(int argc, char **argv) · line 211

Quoted 9 lines
checkpolicy/checkmodule.c +6 -0
@@ -202,6 +203,30 @@ int main(int argc, char **argv)
                case 'C':
                        cil = 1;
                        break;
+               case 'c': {
+                       long int n;
+                       errno = 0;
+                       n = strtol(optarg, NULL, 10);
+
+                       if (errno) {

Get rid of this newline between the strtol() and errno.

Quoted 24 lines
checkpolicy/checkmodule.c +18 -1
+                               fprintf(stderr,
+                                       "Invalid policyvers specified: %s\n",
+                                       optarg);
+                               usage(argv[0]);
+                       }
+
+                       if (n < MOD_POLICYDB_VERSION_MIN
+                           || n > MOD_POLICYDB_VERSION_MAX) {
+                               fprintf(stderr,
+                                       "policyvers value %ld not in range %d-%d\n",
+                                       n, MOD_POLICYDB_VERSION_MIN,
+                                       MOD_POLICYDB_VERSION_MAX);
+                               usage(argv[0]);
+                       }
+
+                       policyvers = n;
+                       break;
+               }
                default:
                        usage(argv[0]);
                }
--

2.17.2

#3 lore raw

PATCH 2/2 dismod: print policy version of loaded modules

In reply to Gary Tierney
---
 checkpolicy/test/dismod.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
checkpolicy/test/dismod.c +2 -2
index d5c7eeab093a..075bd85df4e6 100644
@@ -903,14 +903,14 @@ int main(int argc, char **argv)
 	}
 
 	if (policydb.policy_type == POLICY_BASE) {
-		printf("Binary base policy file loaded.\n\n");
+		printf("Binary base policy file loaded.\n");
 	} else {
 		printf("Binary policy module file loaded.\n");
 		printf("Module name: %s\n", policydb.name);
 		printf("Module version: %s\n", policydb.version);
-		printf("\n");
 	}
 
+	printf("Policy version: %d\n\n", policydb.policyvers);
 	menu();
 	for (;;) {
 		printf("\nCommand (\'m\' for menu):  ");
Signature · 1 line
2.17.2
#4 lore raw

Re: [PATCH 2/2] dismod: print policy version of loaded modules

Quoted 28 lines · Gary Tierney
---
 checkpolicy/test/dismod.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
checkpolicy/test/dismod.c +2 -3
index d5c7eeab093a..075bd85df4e6 100644
@@ -903,14 +903,14 @@ int main(int argc, char **argv)
        }
 
        if (policydb.policy_type == POLICY_BASE) {
-               printf("Binary base policy file loaded.\n\n");
+               printf("Binary base policy file loaded.\n");
        } else {
                printf("Binary policy module file loaded.\n");
                printf("Module name: %s\n", policydb.name);
                printf("Module version: %s\n", policydb.version);
-               printf("\n");
        }
 
+       printf("Policy version: %d\n\n", policydb.policyvers);
        menu();
        for (;;) {
                printf("\nCommand (\'m\' for menu):  ");
--
Quoted 2 lines

2.17.2

I don't have a problem with this, does anyone else?

Acked-by on the v1 series
#5 lore raw
In reply to Gary Tierney
Quoted 30 lines · Gary Tierney

These changes come from a report by a user on the Freenode IRC channel that they were unable to build policies for a machine that has an older version of libsepol installed.

A new -c option that mirrors checkpolicy's own has been added to checkmodule, and the output of a simple test is shown below:

$ cat > test.te <<EOF
module test 1.0;
require {
     type domain;
     type file_type;
     class file { read write };
}

allow domain file_type : file { read write };
EOF

$ obj/usr/bin/checkmodule -m -M -c 10 -o test.mod test.te
$ checkpolicy/test/dismod test.mod
Reading policy...
... snip ...
Binary policy module file loaded.
Module name: test
Module version: 1.0
Policy version: 10

Worthy of note, however, is that these policy version numbers differ from those used by the kernel policy format.

Quoted 9 lines

Gary Tierney (2):
checkmodule: add support for specifying module policy version
dismod: print policy version of loaded modules

  checkpolicy/checkmodule.8 |  5 ++++-
  checkpolicy/checkmodule.c | 29 +++++++++++++++++++++++++++--
  checkpolicy/test/dismod.c |  4 ++--
  3 files changed, 33 insertions(+), 5 deletions(-)
Signature · 2 lines
James Carter <[email protected]>
National Security Agency
#6 lore raw
Quoted 36 lines · On Thu, Apr 18, 2019 at 6:27 AM jwcart2 <[email protected]> wrote:
Quoted 34 lines · Gary Tierney

These changes come from a report by a user on the Freenode IRC channel that they were unable to build policies for a machine that has an older version of libsepol installed.

A new -c option that mirrors checkpolicy's own has been added to checkmodule, and the output of a simple test is shown below:

$ cat > test.te <<EOF
module test 1.0;
require {
     type domain;
     type file_type;
     class file { read write };
}

allow domain file_type : file { read write };
EOF

$ obj/usr/bin/checkmodule -m -M -c 10 -o test.mod test.te
$ checkpolicy/test/dismod test.mod
Reading policy...
... snip ...
Binary policy module file loaded.
Module name: test
Module version: 1.0
Policy version: 10

Worthy of note, however, is that these policy version numbers differ from those used by the kernel policy format.

Gary Tierney (2):
checkmodule: add support for specifying module policy version
dismod: print policy version of loaded modules

Quoted 7 lines
Quoted 5 lines
  checkpolicy/checkmodule.8 |  5 ++++-
  checkpolicy/checkmodule.c | 29 +++++++++++++++++++++++++++--
  checkpolicy/test/dismod.c |  4 ++--
  3 files changed, 33 insertions(+), 5 deletions(-)

James there's a superfluous newline between strtol() and errno.

Quoted 4 lines

--
James Carter <[email protected]>
National Security Agency

#7 lore raw
Quoted 43 lines · William Roberts
Quoted 42 lines · On Thu, Apr 18, 2019 at 6:27 AM jwcart2 <[email protected]> wrote:
Quoted 39 lines · Gary Tierney

These changes come from a report by a user on the Freenode IRC channel that they were unable to build policies for a machine that has an older version of libsepol installed.

A new -c option that mirrors checkpolicy's own has been added to checkmodule, and the output of a simple test is shown below:

$ cat > test.te <<EOF
module test 1.0;
require {
      type domain;
      type file_type;
      class file { read write };
}

allow domain file_type : file { read write };
EOF

$ obj/usr/bin/checkmodule -m -M -c 10 -o test.mod test.te
$ checkpolicy/test/dismod test.mod
Reading policy...
... snip ...
Binary policy module file loaded.
Module name: test
Module version: 1.0
Policy version: 10

Worthy of note, however, is that these policy version numbers differ from those used by the kernel policy format.

Gary Tierney (2):
checkmodule: add support for specifying module policy version
dismod: print policy version of loaded modules

   checkpolicy/checkmodule.8 |  5 ++++-
   checkpolicy/checkmodule.c | 29 +++++++++++++++++++++++++++--
   checkpolicy/test/dismod.c |  4 ++--
   3 files changed, 33 insertions(+), 5 deletions(-)
Quoted 4 lines
Quoted 1 line

James there's a superfluous newline between strtol() and errno.

Thanks, I missed that.

I don't see the need for another version of the patches. I can fix that minor issue when I merge the patches tomorrow.

Quoted 5 lines
Quoted 4 lines

--
James Carter <[email protected]>
National Security Agency

Signature · 2 lines
James Carter <[email protected]>
National Security Agency
#8 lore raw
Quoted 44 lines · On 4/18/19 1:49 PM, jwcart2 wrote:
Quoted 43 lines · William Roberts
Quoted 42 lines · On Thu, Apr 18, 2019 at 6:27 AM jwcart2 <[email protected]> wrote:
Quoted 39 lines · Gary Tierney

These changes come from a report by a user on the Freenode IRC channel that they were unable to build policies for a machine that has an older version of libsepol installed.

A new -c option that mirrors checkpolicy's own has been added to checkmodule, and the output of a simple test is shown below:

$ cat > test.te <<EOF
module test 1.0;

require {
      type domain;
      type file_type;
      class file { read write };
}

allow domain file_type : file { read write };
EOF

$ obj/usr/bin/checkmodule -m -M -c 10 -o test.mod test.te
$ checkpolicy/test/dismod test.mod
Reading policy...
... snip ...
Binary policy module file loaded.
Module name: test
Module version: 1.0
Policy version: 10

Worthy of note, however, is that these policy version numbers differ from those used by the kernel policy format.

Gary Tierney (2):
    checkmodule: add support for specifying module policy version
    dismod: print policy version of loaded modules

   checkpolicy/checkmodule.8 |  5 ++++-
   checkpolicy/checkmodule.c | 29 +++++++++++++++++++++++++++--
   checkpolicy/test/dismod.c |  4 ++--
   3 files changed, 33 insertions(+), 5 deletions(-)
Quoted 9 lines
Quoted 4 lines
Quoted 1 line

James there's a superfluous newline between strtol() and errno.

Thanks, I missed that.

I don't see the need for another version of the patches. I can fix that minor issue when I merge the patches tomorrow.

Merged with the extra newline removed.

Quoted 7 lines
Quoted 5 lines
Quoted 4 lines
Signature · 3 lines
-- 
James Carter <[email protected]>
National Security Agency
Signature · 2 lines
James Carter <[email protected]>
National Security Agency