semodule_link: update

Drop unnecessary declarations.
More verbose error messages and add missing trailing newline.
More strict argument count checking.
Check closing file for incomplete write.
Rework resource cleanup, so that all files and allocated memory are
released in all branches, useful to minimize reports while debugging
libsepol under valgrind(8) or sanitizers.
Add help argument option -h.
Set close-on-exec flag in case of any sibling thread.

Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
Acked-by: James Carter <jwcart2@gmail.com>
This commit is contained in:
Christian Göttsche 2023-07-06 16:53:33 +02:00 committed by James Carter
parent 2b89a35fcc
commit 63e798a203
2 changed files with 53 additions and 39 deletions

View File

@ -3,7 +3,7 @@
semodule_link \- Link SELinux policy module packages together semodule_link \- Link SELinux policy module packages together
.SH SYNOPSIS .SH SYNOPSIS
.B semodule_link [-Vv] [-o outfile] basemodpkg modpkg1 [modpkg2]... .B semodule_link [-hVv] [-o outfile] basemodpkg modpkg1 [modpkg2]...
.br .br
.SH DESCRIPTION .SH DESCRIPTION
.PP .PP
@ -16,6 +16,9 @@ semodule_package.
.SH "OPTIONS" .SH "OPTIONS"
.TP .TP
.B \-h
show help
.TP
.B \-V .B \-V
show version show version
.TP .TP

View File

@ -21,18 +21,13 @@
#define LINKPOLICY_VERSION "1.0" #define LINKPOLICY_VERSION "1.0"
char *progname; static void usage(const char *program_name)
extern char *optarg;
extern int optind;
static __attribute__((__noreturn__)) void usage(const char *program_name)
{ {
printf("usage: %s [-Vv] [-o outfile] basemodpkg modpkg1 [modpkg2]...\n", printf("usage: %s [-hVv] [-o outfile] basemodpkg modpkg1 [modpkg2]...\n",
program_name); program_name);
exit(1);
} }
static sepol_module_package_t *load_module(char *filename) static sepol_module_package_t *load_module(const char *filename, const char *progname)
{ {
int ret; int ret;
FILE *fp = NULL; FILE *fp = NULL;
@ -47,9 +42,9 @@ static sepol_module_package_t *load_module(char *filename)
fprintf(stderr, "%s: Out of memory\n", progname); fprintf(stderr, "%s: Out of memory\n", progname);
goto bad; goto bad;
} }
fp = fopen(filename, "r"); fp = fopen(filename, "re");
if (!fp) { if (!fp) {
fprintf(stderr, "%s: Could not open package %s: %s", progname, fprintf(stderr, "%s: Could not open package %s: %s\n", progname,
filename, strerror(errno)); filename, strerror(errno));
goto bad; goto bad;
} }
@ -76,16 +71,16 @@ static sepol_module_package_t *load_module(char *filename)
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
int ch, i, show_version = 0, verbose = 0, num_mods; int ch, i, ret, show_version = 0, verbose = 0, num_mods = 0;
char *basename, *outname = NULL; const char *basename, *outname = NULL;
sepol_module_package_t *base, **mods; sepol_module_package_t *base = NULL, **mods = NULL;
FILE *outfile; struct sepol_policy_file *pf = NULL;
struct sepol_policy_file *pf;
progname = argv[0]; while ((ch = getopt(argc, argv, "ho:Vv")) != EOF) {
while ((ch = getopt(argc, argv, "o:Vv")) != EOF) {
switch (ch) { switch (ch) {
case 'h':
usage(argv[0]);
return EXIT_SUCCESS;
case 'V': case 'V':
show_version = 1; show_version = 1;
break; break;
@ -97,80 +92,96 @@ int main(int argc, char **argv)
break; break;
default: default:
usage(argv[0]); usage(argv[0]);
return EXIT_FAILURE;
} }
} }
if (show_version) { if (show_version) {
printf("%s\n", LINKPOLICY_VERSION); printf("%s\n", LINKPOLICY_VERSION);
exit(0); return EXIT_SUCCESS;
} }
/* check args */ /* check args */
if (argc < 3 || !(optind != (argc - 1))) { if (argc < 3 || optind + 2 > argc) {
fprintf(stderr, fprintf(stderr,
"%s: You must provide the base module package and at least one other module package\n", "%s: You must provide the base module package and at least one other module package\n",
argv[0]); argv[0]);
usage(argv[0]); usage(argv[0]);
return EXIT_FAILURE;
} }
basename = argv[optind++]; basename = argv[optind++];
base = load_module(basename); base = load_module(basename, argv[0]);
if (!base) { if (!base) {
fprintf(stderr, fprintf(stderr,
"%s: Could not load base module from file %s\n", "%s: Could not load base module from file %s\n",
argv[0], basename); argv[0], basename);
exit(1); goto failure;
} }
num_mods = argc - optind; num_mods = argc - optind;
mods = mods = calloc(num_mods, sizeof(sepol_module_package_t *));
(sepol_module_package_t **) malloc(sizeof(sepol_module_package_t *)
* num_mods);
if (!mods) { if (!mods) {
fprintf(stderr, "%s: Out of memory\n", argv[0]); fprintf(stderr, "%s: Out of memory\n", argv[0]);
exit(1); goto failure;
} }
memset(mods, 0, sizeof(sepol_module_package_t *) * num_mods);
for (i = 0; optind < argc; optind++, i++) { for (i = 0; optind < argc; optind++, i++) {
mods[i] = load_module(argv[optind]); mods[i] = load_module(argv[optind], argv[0]);
if (!mods[i]) { if (!mods[i]) {
fprintf(stderr, fprintf(stderr,
"%s: Could not load module from file %s\n", "%s: Could not load module from file %s\n",
argv[0], argv[optind]); argv[0], argv[optind]);
exit(1); goto failure;
} }
} }
if (sepol_link_packages(NULL, base, mods, num_mods, verbose)) { if (sepol_link_packages(NULL, base, mods, num_mods, verbose)) {
fprintf(stderr, "%s: Error while linking packages\n", argv[0]); fprintf(stderr, "%s: Error while linking packages\n", argv[0]);
exit(1); goto failure;
} }
if (outname) { if (outname) {
outfile = fopen(outname, "w"); FILE *outfile = fopen(outname, "we");
if (!outfile) { if (!outfile) {
perror(outname); fprintf(stderr, "%s: Could not open output file %s: %s\n",
exit(1); argv[0], outname, strerror(errno));
goto failure;
} }
if (sepol_policy_file_create(&pf)) { if (sepol_policy_file_create(&pf)) {
fprintf(stderr, "%s: Out of memory\n", argv[0]); fprintf(stderr, "%s: Out of memory\n", argv[0]);
exit(1); fclose(outfile);
goto failure;
} }
sepol_policy_file_set_fp(pf, outfile); sepol_policy_file_set_fp(pf, outfile);
if (sepol_module_package_write(base, pf)) { if (sepol_module_package_write(base, pf)) {
fprintf(stderr, "%s: Error writing linked package.\n", fprintf(stderr, "%s: Error writing linked package.\n",
argv[0]); argv[0]);
exit(1); sepol_policy_file_free(pf);
fclose(outfile);
goto failure;
} }
sepol_policy_file_free(pf); sepol_policy_file_free(pf);
fclose(outfile);
if (fclose(outfile)) {
fprintf(stderr, "%s: Error closing linked package: %s\n",
argv[0], strerror(errno));
goto failure;
}
} }
sepol_module_package_free(base); ret = EXIT_SUCCESS;
goto cleanup;
failure:
ret = EXIT_FAILURE;
cleanup:
for (i = 0; i < num_mods; i++) for (i = 0; i < num_mods; i++)
sepol_module_package_free(mods[i]); sepol_module_package_free(mods[i]);
free(mods); free(mods);
exit(0); sepol_module_package_free(base);
return ret;
} }