Page Menu
Home
FreeBSD
Search
Configure Global Search
Log In
Files
F177836864
D59580.1795396974.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Authored By
Unknown
Size
21 KB
Referenced Files
None
Subscribers
None
D59580.1795396974.diff
View Options
diff --git a/usr.sbin/uefisign/child.c b/usr.sbin/uefisign/child.c
--- a/usr.sbin/uefisign/child.c
+++ b/usr.sbin/uefisign/child.c
@@ -40,6 +40,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <sysexits.h>
#include <unistd.h>
#include <openssl/evp.h>
@@ -60,19 +61,19 @@
error = fstat(fd, &sb);
if (error != 0)
- err(1, "%s: fstat", x->x_path);
+ err(EX_OSERR, "%s: fstat", x->x_path);
len = sb.st_size;
if (len <= 0)
- errx(1, "%s: file is empty", x->x_path);
+ errx(EXIT_MALFORMED, "%s: file is empty", x->x_path);
buf = malloc(len);
if (buf == NULL)
- err(1, "%s: cannot malloc %zd bytes", x->x_path, len);
+ err(EX_OSERR, "%s: cannot malloc %zd bytes", x->x_path, len);
nread = fread(buf, len, 1, x->x_fp);
if (nread != 1)
- err(1, "%s: fread", x->x_path);
+ err(EX_IOERR, "%s: fread", x->x_path);
x->x_buf = buf;
x->x_len = len;
@@ -88,7 +89,7 @@
ok = EVP_DigestUpdate(mdctx, x->x_buf + off, len);
if (ok == 0) {
ERR_print_errors_fp(stderr);
- errx(1, "EVP_DigestUpdate(3) failed");
+ errx(EX_SOFTWARE, "EVP_DigestUpdate(3) failed");
}
}
@@ -108,19 +109,20 @@
md = EVP_get_digestbyname(DIGEST);
if (md == NULL) {
ERR_print_errors_fp(stderr);
- errx(1, "EVP_get_digestbyname(\"%s\") failed", DIGEST);
+ errx(EX_UNAVAILABLE, "EVP_get_digestbyname(\"%s\") failed",
+ DIGEST);
}
mdctx = EVP_MD_CTX_create();
if (mdctx == NULL) {
ERR_print_errors_fp(stderr);
- errx(1, "EVP_MD_CTX_create(3) failed");
+ errx(EX_SOFTWARE, "EVP_MD_CTX_create(3) failed");
}
ok = EVP_DigestInit_ex(mdctx, md, NULL);
if (ok == 0) {
ERR_print_errors_fp(stderr);
- errx(1, "EVP_DigestInit_ex(3) failed");
+ errx(EX_SOFTWARE, "EVP_DigestInit_ex(3) failed");
}
/*
@@ -164,7 +166,8 @@
* I believe this can happen with overlapping sections.
*/
if (sum_of_bytes_hashed > x->x_len)
- errx(1, "number of bytes hashed is larger than file size");
+ errx(EXIT_MALFORMED,
+ "number of bytes hashed is larger than file size");
/*
* I can't really explain this one; just do what the spec says.
@@ -177,7 +180,7 @@
ok = EVP_DigestFinal_ex(mdctx, x->x_digest, &x->x_digest_len);
if (ok == 0) {
ERR_print_errors_fp(stderr);
- errx(1, "EVP_DigestFinal_ex(3) failed");
+ errx(EX_SOFTWARE, "EVP_DigestFinal_ex(3) failed");
}
EVP_MD_CTX_destroy(mdctx);
@@ -218,7 +221,7 @@
nwritten = fwrite(x->x_buf, x->x_len, 1, fp);
if (nwritten != 1)
- err(1, "%s: fwrite", path);
+ err(EX_IOERR, "%s: fwrite", path);
}
int
@@ -228,16 +231,16 @@
FILE *outfp = NULL, *infp = NULL;
struct executable *x;
- infp = checked_fopen(inpath, "r");
+ infp = checked_fopen(inpath, "r", EX_NOINPUT);
if (outpath != NULL)
- outfp = checked_fopen(outpath, "w");
+ outfp = checked_fopen(outpath, "w", EX_CANTCREAT);
if (caph_enter() < 0)
- err(1, "cap_enter");
+ err(EX_OSERR, "cap_enter");
x = calloc(1, sizeof(*x));
if (x == NULL)
- err(1, "calloc");
+ err(EX_OSERR, "calloc");
x->x_path = inpath;
x->x_fp = infp;
@@ -245,7 +248,7 @@
parse(x);
if (Vflag) {
if (signature_size(x) == 0)
- errx(1, "file not signed");
+ errx(EXIT_SIGNATURE, "file not signed");
printf("file contains signature\n");
if (vflag) {
@@ -255,7 +258,7 @@
}
} else {
if (signature_size(x) != 0)
- errx(1, "file already signed");
+ errx(EXIT_SIGNATURE, "file already signed");
digest(x);
if (vflag)
@@ -266,5 +269,5 @@
save(x, outfp, outpath);
}
- return (0);
+ return (EX_OK);
}
diff --git a/usr.sbin/uefisign/pe.c b/usr.sbin/uefisign/pe.c
--- a/usr.sbin/uefisign/pe.c
+++ b/usr.sbin/uefisign/pe.c
@@ -43,6 +43,7 @@
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
+#include <sysexits.h>
#include <unistd.h>
#include "uefisign.h"
@@ -144,19 +145,22 @@
{
if (off < 0) {
- errx(1, "%s starts at negative offset %jd",
+ errx(EXIT_MALFORMED, "%s starts at negative offset %jd",
name, (intmax_t)off);
}
if (off >= (off_t)x->x_len) {
- errx(1, "%s starts at %jd, past the end of executable at %zd",
+ errx(EXIT_MALFORMED,
+ "%s starts at %jd, past the end of executable at %zd",
name, (intmax_t)off, x->x_len);
}
if (len >= x->x_len) {
- errx(1, "%s size %zd is larger than the executable size %zd",
+ errx(EXIT_MALFORMED,
+ "%s size %zd is larger than the executable size %zd",
name, len, x->x_len);
}
if (off + len > x->x_len) {
- errx(1, "%s extends to %jd, past the end of executable at %zd",
+ errx(EXIT_MALFORMED,
+ "%s extends to %jd, past the end of executable at %zd",
name, (intmax_t)(off + len), x->x_len);
}
}
@@ -206,11 +210,13 @@
pc = (struct pe_certificate *)(x->x_buf + pde->pde_rva);
if (pc->pc_revision != PE_CERTIFICATE_REVISION) {
- errx(1, "wrong certificate chunk revision, is %d, should be %d",
+ errx(EXIT_MALFORMED,
+ "wrong certificate chunk revision, is %d, should be %d",
pc->pc_revision, PE_CERTIFICATE_REVISION);
}
if (pc->pc_type != PE_CERTIFICATE_TYPE) {
- errx(1, "wrong certificate chunk type, is %d, should be %d",
+ errx(EXIT_MALFORMED,
+ "wrong certificate chunk type, is %d, should be %d",
pc->pc_type, PE_CERTIFICATE_TYPE);
}
printf("to dump PKCS7:\n "
@@ -231,12 +237,12 @@
"section table");
if (x->x_headers_len < off + sizeof(*psh) * number_of_sections)
- errx(1, "section table outside of headers");
+ errx(EXIT_MALFORMED, "section table outside of headers");
psh = (const struct pe_section_header *)(x->x_buf + off);
if (number_of_sections >= MAX_SECTIONS) {
- errx(1, "too many sections: got %d, should be %d",
+ errx(EXIT_MALFORMED, "too many sections: got %d, should be %d",
number_of_sections, MAX_SECTIONS);
}
x->x_nsections = number_of_sections;
@@ -244,7 +250,8 @@
for (i = 0; i < number_of_sections; i++) {
if (psh->psh_size_of_raw_data > 0 &&
psh->psh_pointer_to_raw_data < x->x_headers_len)
- errx(1, "section points inside the headers");
+ errx(EXIT_MALFORMED,
+ "section points inside the headers");
range_check(x, psh->psh_pointer_to_raw_data,
psh->psh_size_of_raw_data, "section");
@@ -268,14 +275,15 @@
//printf("Data Directory at offset %zd\n", off);
if (number_of_rva_and_sizes <= PE_DIRECTORY_ENTRY_CERTIFICATE) {
- errx(1, "wrong NumberOfRvaAndSizes %d; should be at least %d",
+ errx(EXIT_MALFORMED,
+ "wrong NumberOfRvaAndSizes %d; should be at least %d",
number_of_rva_and_sizes, PE_DIRECTORY_ENTRY_CERTIFICATE);
}
range_check(x, off, sizeof(*pde) * number_of_rva_and_sizes,
"PE Data Directory");
if (x->x_headers_len <= off + sizeof(*pde) * number_of_rva_and_sizes)
- errx(1, "PE Data Directory outside of headers");
+ errx(EXIT_MALFORMED, "PE Data Directory outside of headers");
x->x_certificate_entry_off =
off + sizeof(*pde) * PE_DIRECTORY_ENTRY_CERTIFICATE;
@@ -362,7 +370,7 @@
case PE_OPTIONAL_SUBSYSTEM_EFI_RUNTIME:
break;
default:
- errx(1, "wrong PE Optional Header subsystem 0x%x",
+ errx(EXIT_MALFORMED, "wrong PE Optional Header subsystem 0x%x",
po->po_subsystem);
}
@@ -386,7 +394,8 @@
#endif
if (x->x_len < x->x_headers_len)
- errx(1, "invalid SizeOfHeaders %d", po->po_size_of_headers);
+ errx(EXIT_MALFORMED, "invalid SizeOfHeaders %d",
+ po->po_size_of_headers);
x->x_headers_len = po->po_size_of_headers;
//printf("Size of Headers: %d\n", po->po_size_of_headers);
@@ -411,7 +420,7 @@
case PE_OPTIONAL_SUBSYSTEM_EFI_RUNTIME:
break;
default:
- errx(1, "wrong PE Optional Header subsystem 0x%x",
+ errx(EXIT_MALFORMED, "wrong PE Optional Header subsystem 0x%x",
po->po_subsystem);
}
@@ -435,7 +444,8 @@
#endif
if (x->x_len < x->x_headers_len)
- errx(1, "invalid SizeOfHeaders %d", po->po_size_of_headers);
+ errx(EXIT_MALFORMED, "invalid SizeOfHeaders %d",
+ po->po_size_of_headers);
x->x_headers_len = po->po_size_of_headers;
//printf("Size of Headers: %d\n", po->po_size_of_headers);
@@ -460,7 +470,8 @@
case PE_OPTIONAL_MAGIC_32_PLUS:
return (parse_optional_32_plus(x, off, number_of_sections));
default:
- errx(1, "wrong PE Optional Header magic 0x%x", po->po_magic);
+ errx(EXIT_MALFORMED, "wrong PE Optional Header magic 0x%x",
+ po->po_magic);
}
}
@@ -475,7 +486,8 @@
pe = (struct pe_header *)(x->x_buf + off);
if (pe->pe_signature != PE_SIGNATURE)
- errx(1, "wrong PE signature 0x%x", pe->pe_signature);
+ errx(EXIT_MALFORMED, "wrong PE signature 0x%x",
+ pe->pe_signature);
//printf("Number of sections: %d\n", pe->pe_coff.coff_number_of_sections);
@@ -492,7 +504,7 @@
mz = (struct mz_header *)x->x_buf;
if (mz->mz_signature[0] != 'M' || mz->mz_signature[1] != 'Z')
- errx(1, "MZ header not found");
+ errx(EXIT_MALFORMED, "MZ header not found");
return (parse_pe(x, mz->mz_lfanew));
}
@@ -505,7 +517,7 @@
off = x->x_len;
x->x_buf = realloc(x->x_buf, x->x_len + len + aligment);
if (x->x_buf == NULL)
- err(1, "realloc");
+ err(EX_OSERR, "realloc");
memcpy(x->x_buf + x->x_len, ptr, len);
memset(x->x_buf + x->x_len + len, 0, aligment);
x->x_len += len + aligment;
@@ -526,7 +538,7 @@
pc_len = sizeof(*pc) + x->x_signature_len;
pc = calloc(1, pc_len);
if (pc == NULL)
- err(1, "calloc");
+ err(EX_OSERR, "calloc");
if (pc_len % PE_ALIGMENT_SIZE > 0)
pc_aligment = PE_ALIGMENT_SIZE - (pc_len % PE_ALIGMENT_SIZE);
diff --git a/usr.sbin/uefisign/uefisign.h b/usr.sbin/uefisign/uefisign.h
--- a/usr.sbin/uefisign/uefisign.h
+++ b/usr.sbin/uefisign/uefisign.h
@@ -37,6 +37,14 @@
#define DIGEST "SHA256"
#define MAX_SECTIONS 128
+/*
+ * Exit status. A small value is a statement about the file that was
+ * examined; a failure of uefisign itself is reported with a value from
+ * <sysexits.h>.
+ */
+#define EXIT_SIGNATURE 1 /* wrong signature state for the operation */
+#define EXIT_MALFORMED 2 /* not a conforming UEFI PE image */
+
struct executable {
const char *x_path;
FILE *x_fp;
@@ -73,7 +81,7 @@
};
-FILE *checked_fopen(const char *path, const char *mode);
+FILE *checked_fopen(const char *path, const char *mode, int eval);
void send_chunk(const void *buf, size_t len, int pipefd);
void receive_chunk(void **bufp, size_t *lenp, int pipefd);
diff --git a/usr.sbin/uefisign/uefisign.8 b/usr.sbin/uefisign/uefisign.8
--- a/usr.sbin/uefisign/uefisign.8
+++ b/usr.sbin/uefisign/uefisign.8
@@ -24,7 +24,7 @@
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
-.Dd July 11, 2015
+.Dd September 10, 2026
.Dt UEFISIGN 8
.Os
.Sh NAME
@@ -63,9 +63,61 @@
Be verbose.
.El
.Sh EXIT STATUS
-The
+An exit status of 0 means that the requested operation succeeded:
+the file was signed, or, with
+.Fl V ,
+the file was found to contain a signature.
+.Pp
+A small exit status is a statement about the file that was examined.
+The utility ran correctly; the file is not in the state the requested
+operation needs:
+.Bl -tag -width indent
+.It 1
+The signature state of the file is wrong for the requested operation.
+With
+.Fl V ,
+the file is not signed.
+When signing, the file is already signed.
+.It 2
+The file is not a conforming UEFI PE image.
+This covers a missing MZ or PE signature, an unrecognised Optional Header
+magic, a subsystem that is not an EFI one, a Data Directory or section table
+that does not lie within the headers, an offset or length that falls outside
+the file, and a malformed certificate chunk.
+.El
+.Pp
+An exit status of 64 or above means that
.Nm
-utility exits 0 on success, and >0 if an error occurs.
+itself failed and reached no conclusion about the file.
+These values are taken from
+.Xr sysexits 3 :
+.Bl -tag -width ".Dv EX_UNAVAILABLE"
+.It Dv EX_USAGE Pq 64
+The command line was wrong.
+.It Dv EX_DATAERR Pq 65
+The certificate or the private key could not be parsed.
+.It Dv EX_NOINPUT Pq 66
+The file to be examined, the certificate or the key could not be opened.
+.It Dv EX_UNAVAILABLE Pq 69
+The cryptographic library does not provide the required digest algorithm.
+.It Dv EX_SOFTWARE Pq 70
+A cryptographic library call failed.
+.It Dv EX_OSERR Pq 71
+A system call failed, or memory could not be allocated.
+.It Dv EX_CANTCREAT Pq 73
+The output file could not be created.
+.It Dv EX_IOERR Pq 74
+Reading the input, writing the output, or communicating with the child
+process failed.
+.El
+.Pp
+The file is read and parsed by a separate, capability mode process.
+If that process is terminated by a signal,
+.Nm
+exits with 128 plus the number of that signal.
+An exit status of 0 therefore never means that
+.Nm
+crashed, failed an assertion or ran into a resource limit.
.Sh EXAMPLES
Generate self-signed certificate and use it to sign a binary:
.Dl /usr/share/examples/uefisign/uefikeys testcert
@@ -75,6 +127,7 @@
.Dl uefisign -Vv binary
.Sh SEE ALSO
.Xr openssl 1 ,
+.Xr sysexits 3 ,
.Xr loader 8 ,
.Xr uefi 8
.Sh HISTORY
@@ -82,6 +135,11 @@
.Nm
command appeared in
.Fx 10.2 .
+.Pp
+Before
+.Fx 16.0
+every failure was reported with an exit status of 1, and a signal that killed
+the process parsing the file was reported as an exit status of 0.
.Sh AUTHORS
The
.Nm
diff --git a/usr.sbin/uefisign/uefisign.c b/usr.sbin/uefisign/uefisign.c
--- a/usr.sbin/uefisign/uefisign.c
+++ b/usr.sbin/uefisign/uefisign.c
@@ -34,8 +34,10 @@
#include <assert.h>
#include <err.h>
#include <errno.h>
+#include <signal.h>
#include <stdio.h>
#include <string.h>
+#include <sysexits.h>
#include <unistd.h>
#include <openssl/conf.h>
@@ -53,7 +55,7 @@
fprintf(stderr, "usage: uefisign -c cert -k key -o outfile [-v] file\n"
" uefisign -V [-c cert] [-v] file\n");
- exit(1);
+ exit(EX_USAGE);
}
static char *
@@ -63,12 +65,12 @@
c = strdup(s);
if (c == NULL)
- err(1, "strdup");
+ err(EX_OSERR, "strdup");
return (c);
}
FILE *
-checked_fopen(const char *path, const char *mode)
+checked_fopen(const char *path, const char *mode, int eval)
{
FILE *fp;
@@ -76,21 +78,43 @@
fp = fopen(path, mode);
if (fp == NULL)
- err(1, "%s", path);
+ err(eval, "%s", path);
return (fp);
}
+static pid_t child_pid = -1;
+
+static int wait_for_child(pid_t pid);
+
+static void
+peer_exited(void)
+{
+ int status;
+
+ if (child_pid == -1)
+ exit(EX_IOERR);
+
+ status = wait_for_child(child_pid);
+ if (status == EX_OK)
+ errx(EX_SOFTWARE, "child exited without sending its result");
+ exit(status);
+}
+
void
send_chunk(const void *buf, size_t len, int pipefd)
{
ssize_t ret;
ret = write(pipefd, &len, sizeof(len));
+ if (ret == -1 && errno == EPIPE)
+ peer_exited();
if (ret != sizeof(len))
- err(1, "write");
+ err(EX_IOERR, "write");
ret = write(pipefd, buf, len);
+ if (ret == -1 && errno == EPIPE)
+ peer_exited();
if (ret != (ssize_t)len)
- err(1, "write");
+ err(EX_IOERR, "write");
}
void
@@ -101,16 +125,20 @@
void *buf;
ret = read(pipefd, &len, sizeof(len));
+ if (ret == 0)
+ peer_exited();
if (ret != sizeof(len))
- err(1, "read");
+ err(EX_IOERR, "read");
buf = calloc(1, len);
if (buf == NULL)
- err(1, "calloc");
+ err(EX_OSERR, "calloc");
ret = read(pipefd, buf, len);
+ if (ret == 0)
+ peer_exited();
if (ret != (ssize_t)len)
- err(1, "read");
+ err(EX_IOERR, "read");
*bufp = buf;
*lenp = len;
@@ -126,7 +154,7 @@
hex_len = bin_len * 2 + 1; /* +1 for '\0'. */
hex = malloc(hex_len);
if (hex == NULL)
- err(1, "malloc");
+ err(EX_OSERR, "malloc");
tmp = hex;
for (i = 0; i < bin_len; i++) {
@@ -167,36 +195,36 @@
asprintf(&magic_conf, magic_fmt, digest_hex);
if (magic_conf == NULL)
- err(1, "asprintf");
+ err(EX_OSERR, "asprintf");
bio = BIO_new_mem_buf((void *)magic_conf, -1);
if (bio == NULL) {
ERR_print_errors_fp(stderr);
- errx(1, "BIO_new_mem_buf(3) failed");
+ errx(EX_SOFTWARE, "BIO_new_mem_buf(3) failed");
}
cnf = NCONF_new(NULL);
if (cnf == NULL) {
ERR_print_errors_fp(stderr);
- errx(1, "NCONF_new(3) failed");
+ errx(EX_SOFTWARE, "NCONF_new(3) failed");
}
ok = NCONF_load_bio(cnf, bio, NULL);
if (ok == 0) {
ERR_print_errors_fp(stderr);
- errx(1, "NCONF_load_bio(3) failed");
+ errx(EX_SOFTWARE, "NCONF_load_bio(3) failed");
}
str = NCONF_get_string(cnf, "default", "asn1");
if (str == NULL) {
ERR_print_errors_fp(stderr);
- errx(1, "NCONF_get_string(3) failed");
+ errx(EX_SOFTWARE, "NCONF_get_string(3) failed");
}
t = ASN1_generate_nconf(str, cnf);
if (t == NULL) {
ERR_print_errors_fp(stderr);
- errx(1, "ASN1_generate_nconf(3) failed");
+ errx(EX_SOFTWARE, "ASN1_generate_nconf(3) failed");
}
/*
@@ -206,7 +234,7 @@
len = i2d_ASN1_TYPE(t, NULL);
tmp = buf = calloc(1, len);
if (tmp == NULL)
- err(1, "calloc");
+ err(EX_OSERR, "calloc");
i2d_ASN1_TYPE(t, &tmp);
/*
@@ -218,7 +246,7 @@
t_bio = PKCS7_dataInit(pkcs7, NULL);
if (t_bio == NULL) {
ERR_print_errors_fp(stderr);
- errx(1, "PKCS7_dataInit(3) failed");
+ errx(EX_SOFTWARE, "PKCS7_dataInit(3) failed");
}
BIO_write(t_bio, buf + 2, len - 2);
@@ -226,7 +254,7 @@
ok = PKCS7_dataFinal(pkcs7, t_bio);
if (ok == 0) {
ERR_print_errors_fp(stderr);
- errx(1, "PKCS7_dataFinal(3) failed");
+ errx(EX_SOFTWARE, "PKCS7_dataFinal(3) failed");
}
t = ASN1_TYPE_new();
@@ -256,25 +284,26 @@
bio = BIO_new_mem_buf(digest, digest_len);
if (bio == NULL) {
ERR_print_errors_fp(stderr);
- errx(1, "BIO_new_mem_buf(3) failed");
+ errx(EX_SOFTWARE, "BIO_new_mem_buf(3) failed");
}
pkcs7 = PKCS7_sign(NULL, NULL, NULL, bio, PKCS7_BINARY | PKCS7_PARTIAL);
if (pkcs7 == NULL) {
ERR_print_errors_fp(stderr);
- errx(1, "PKCS7_sign(3) failed");
+ errx(EX_SOFTWARE, "PKCS7_sign(3) failed");
}
md = EVP_get_digestbyname(DIGEST);
if (md == NULL) {
ERR_print_errors_fp(stderr);
- errx(1, "EVP_get_digestbyname(\"%s\") failed", DIGEST);
+ errx(EX_UNAVAILABLE, "EVP_get_digestbyname(\"%s\") failed",
+ DIGEST);
}
info = PKCS7_sign_add_signer(pkcs7, cert, key, md, 0);
if (info == NULL) {
ERR_print_errors_fp(stderr);
- errx(1, "PKCS7_sign_add_signer(3) failed");
+ errx(EX_SOFTWARE, "PKCS7_sign_add_signer(3) failed");
}
/*
@@ -297,19 +326,19 @@
out = BIO_new(BIO_s_mem());
if (out == NULL) {
ERR_print_errors_fp(stderr);
- errx(1, "BIO_new(3) failed");
+ errx(EX_SOFTWARE, "BIO_new(3) failed");
}
ok = i2d_PKCS7_bio(out, pkcs7);
if (ok == 0) {
ERR_print_errors_fp(stderr);
- errx(1, "i2d_PKCS7_bio(3) failed");
+ errx(EX_SOFTWARE, "i2d_PKCS7_bio(3) failed");
}
signature_len = BIO_get_mem_data(out, &signature);
if (signature_len <= 0) {
ERR_print_errors_fp(stderr);
- errx(1, "BIO_get_mem_data(3) failed");
+ errx(EX_SOFTWARE, "BIO_get_mem_data(3) failed");
}
(void)BIO_set_close(out, BIO_NOCLOSE);
@@ -325,7 +354,16 @@
pid = waitpid(pid, &status, 0);
if (pid == -1)
- err(1, "waitpid");
+ err(EX_OSERR, "waitpid");
+
+ /*
+ * WEXITSTATUS() of a process killed by a signal is 0.
+ */
+ if (WIFSIGNALED(status)) {
+ warnx("child process terminated by signal %d",
+ WTERMSIG(status));
+ return (128 + WTERMSIG(status));
+ }
return (WEXITSTATUS(status));
}
@@ -351,19 +389,22 @@
if (certpath == NULL)
certpath = checked_strdup(optarg);
else
- err(1, "-c can only be specified once");
+ errx(EX_USAGE,
+ "-c can only be specified once");
break;
case 'k':
if (keypath == NULL)
keypath = checked_strdup(optarg);
else
- err(1, "-k can only be specified once");
+ errx(EX_USAGE,
+ "-k can only be specified once");
break;
case 'o':
if (outpath == NULL)
outpath = checked_strdup(optarg);
else
- err(1, "-o can only be specified once");
+ errx(EX_USAGE,
+ "-o can only be specified once");
break;
case 'v':
vflag = true;
@@ -380,18 +421,18 @@
if (Vflag) {
if (certpath != NULL)
- errx(1, "-V and -c are mutually exclusive");
+ errx(EX_USAGE, "-V and -c are mutually exclusive");
if (keypath != NULL)
- errx(1, "-V and -k are mutually exclusive");
+ errx(EX_USAGE, "-V and -k are mutually exclusive");
if (outpath != NULL)
- errx(1, "-V and -o are mutually exclusive");
+ errx(EX_USAGE, "-V and -o are mutually exclusive");
} else {
if (certpath == NULL)
- errx(1, "-c option is mandatory");
+ errx(EX_USAGE, "-c option is mandatory");
if (keypath == NULL)
- errx(1, "-k option is mandatory");
+ errx(EX_USAGE, "-k option is mandatory");
if (outpath == NULL)
- errx(1, "-o option is mandatory");
+ errx(EX_USAGE, "-o option is mandatory");
}
inpath = argv[0];
@@ -402,32 +443,38 @@
error = pipe(pipefds);
if (error != 0)
- err(1, "pipe");
+ err(EX_OSERR, "pipe");
+
+ if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
+ err(EX_OSERR, "signal");
pid = fork();
if (pid < 0)
- err(1, "fork");
+ err(EX_OSERR, "fork");
if (pid == 0) {
close(pipefds[0]);
exit(child(inpath, outpath, pipefds[1], Vflag, vflag));
}
+ child_pid = pid;
close(pipefds[1]);
if (!Vflag) {
- certfp = checked_fopen(certpath, "r");
+ certfp = checked_fopen(certpath, "r", EX_NOINPUT);
cert = PEM_read_X509(certfp, NULL, NULL, NULL);
if (cert == NULL) {
ERR_print_errors_fp(stderr);
- errx(1, "failed to load certificate from %s", certpath);
+ errx(EX_DATAERR, "failed to load certificate from %s",
+ certpath);
}
- keyfp = checked_fopen(keypath, "r");
+ keyfp = checked_fopen(keypath, "r", EX_NOINPUT);
key = PEM_read_PrivateKey(keyfp, NULL, NULL, NULL);
if (key == NULL) {
ERR_print_errors_fp(stderr);
- errx(1, "failed to load private key from %s", keypath);
+ errx(EX_DATAERR, "failed to load private key from %s",
+ keypath);
}
sign(cert, key, pipefds[0]);
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Mon, Nov 23, 1:22 AM (7 h, 30 s)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
41919819
Default Alt Text
D59580.1795396974.diff (21 KB)
Attached To
Mode
D59580: uefisign: Separate file verdicts from failures
Attached
Detach File
Event Timeline