From patchwork Tue Feb 11 00:00:31 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jes Sorensen X-Patchwork-Id: 11374241 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 0DDAE921 for ; Tue, 11 Feb 2020 00:00:51 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id E2F922082F for ; Tue, 11 Feb 2020 00:00:50 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="X90c4z98" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727530AbgBKAAu (ORCPT ); Mon, 10 Feb 2020 19:00:50 -0500 Received: from mail-pf1-f193.google.com ([209.85.210.193]:38794 "EHLO mail-pf1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727490AbgBKAAu (ORCPT ); Mon, 10 Feb 2020 19:00:50 -0500 Received: by mail-pf1-f193.google.com with SMTP id x185so4509823pfc.5 for ; Mon, 10 Feb 2020 16:00:50 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=YUF2HLL14kZytMn37pkAatXRP35mL6xpENUEHb3f2cA=; b=X90c4z98XfN2rd/D+R0+qcbXgVmtGSQBZbP80PEK4W3G7oOy/7UR83qKUvYgHGDZ92 OIlJ2zZ45EBBpC9FxOmA3v6vxpRFUgAqFrEgt0xfCPoNl2ZL9Rgw3FaSS0gk3pQoZ/VE yy5mZn4wgbHJtZ9F6HK+x8mqcOZitQtiT97j8OjSakaYozqu7ysA5W28NbuTfDTTNTNr OJ+Z/1tUbvbuciJlgPlOHiXQmyae8t9FJtAhEsxoX7diV+yo+Ne2Ob5ZVBg8F1yvNN9L VcTdoRjisgdsvyK3CyaSiwDcUvu5ymrEHfa5tHvJ3lciUmpSzDpvjrtIxsJrRy4RGkk+ hD5w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=YUF2HLL14kZytMn37pkAatXRP35mL6xpENUEHb3f2cA=; b=i9a9nTJ70saq4/SGSEspYZgvR1uGjNxe+0wAgK3D09//VBfLK7uywc+cfXtx+FoDnO HMBoNdlW1D5fvxXCUuRzESgXllZq13mRNGf0d5S1SqO3Gud29c2ZRGFibv9XRMKyJH/T 48h5tgN6qQn6/7A+eJjWHpSXWO/h+3XiHMDftD07slMqBy8X05k1DVympRe+8QT9bgEI JoHSDeua/i2aCtWZYTwNDMM5pNZbrZBdjnGNdpK4Yn7iVDMNzbGS+K5vgQ7GrFSwlS9F oEscZtxmzgUZ3eUA6YSOOOfu5t9ZuVDrKkFSryu9yVTLhr7ibMd2nvYL2DodAhNA323B +sTA== X-Gm-Message-State: APjAAAVyDkCUzIF/o9I2tIDNfzNGKZrefW5dWUYflabHcyOPmQBKIWiJ gXugM1gA/ixC8QMB4Y76N/TbseOxsmU= X-Google-Smtp-Source: APXvYqwOl0F6qrNk10qIU7kZCWqei9cwZibdv3/+5AaomSXBYp/0LsUUSFb8Mt4HprydmJmGgsliOg== X-Received: by 2002:a65:43cb:: with SMTP id n11mr4003621pgp.65.1581379249489; Mon, 10 Feb 2020 16:00:49 -0800 (PST) Received: from localhost ([2620:10d:c090:200::6168]) by smtp.gmail.com with ESMTPSA id q187sm1435510pfq.185.2020.02.10.16.00.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Feb 2020 16:00:48 -0800 (PST) From: Jes Sorensen X-Google-Original-From: Jes Sorensen To: linux-fscrypt@vger.kernel.org Cc: kernel-team@fb.com, Jes Sorensen Subject: [PATCH 1/7] Build basic shared library Date: Mon, 10 Feb 2020 19:00:31 -0500 Message-Id: <20200211000037.189180-2-Jes.Sorensen@gmail.com> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200211000037.189180-1-Jes.Sorensen@gmail.com> References: <20200211000037.189180-1-Jes.Sorensen@gmail.com> MIME-Version: 1.0 Sender: linux-fscrypt-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fscrypt@vger.kernel.org From: Jes Sorensen Signed-off-by: Jes Sorensen --- Makefile | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index b9c09b9..006fc60 100644 --- a/Makefile +++ b/Makefile @@ -1,20 +1,32 @@ EXE := fsverity +LIB := libfsverity.so CFLAGS := -O2 -Wall CPPFLAGS := -D_FILE_OFFSET_BITS=64 LDLIBS := -lcrypto DESTDIR := /usr/local +LIBDIR := /usr/lib64 SRC := $(wildcard *.c) -OBJ := $(SRC:.c=.o) +OBJ := fsverity.o +SSRC := hash_algs.c cmd_sign.c cmd_enable.c cmd_measure.c util.c +SOBJ := hash_algs.so cmd_sign.so cmd_enable.so cmd_measure.so util.so HDRS := $(wildcard *.h) all:$(EXE) -$(EXE):$(OBJ) +$(EXE):$(OBJ) $(LIB) + $(CC) -o $@ $< -L . -l fsverity $(OBJ): %.o: %.c $(HDRS) + $(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@ + +$(SOBJ): %.so: %.c $(HDRS) + $(CC) -c -fPIC $(CFLAGS) $(CPPFLAGS) $< -o $@ + +libfsverity.so: $(SOBJ) + $(CC) $(LDLIBS) -shared -o libfsverity.so $(SOBJ) clean: - rm -f $(EXE) $(OBJ) + rm -f $(EXE) $(OBJ) $(SOBJ) $(LIB) install:all install -Dm755 -t $(DESTDIR)/bin $(EXE) From patchwork Tue Feb 11 00:00:32 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jes Sorensen X-Patchwork-Id: 11374243 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 05E0A921 for ; Tue, 11 Feb 2020 00:00:58 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id BD12020733 for ; Tue, 11 Feb 2020 00:00:57 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="ms0wiOM1" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727549AbgBKAA5 (ORCPT ); Mon, 10 Feb 2020 19:00:57 -0500 Received: from mail-pj1-f66.google.com ([209.85.216.66]:39693 "EHLO mail-pj1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727490AbgBKAA5 (ORCPT ); Mon, 10 Feb 2020 19:00:57 -0500 Received: by mail-pj1-f66.google.com with SMTP id e9so448930pjr.4 for ; Mon, 10 Feb 2020 16:00:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=7hEAib8ELxLbUK8nJSpAmW9igi7majswKHW1Qhm97zw=; b=ms0wiOM12RH2L35dkDNy2Zudo27lSsITpYI3AxGFM0RN7sHpc9bFpAqFijpwHbXd1b to93fN3iIoi/Dy7WuZ4o4W98uMxbLnWOTEjmD+LXHnc6vZ9MF12GaPAhIZcQgoZlOYrP PwG/7RAdT2ltj80fvgIqFtRcmT9Wg9nKaEwWuE/yAlk0WQsvilpA/DVJDt/T5VWpVQ8j Xuxj92ti6WsuafWLstz3H1lj/WCZ3bjhBeytY1rSZpq9QYkHKX2aW47//h8VzMmbFCmB CRAcQyedUAtT+oWbhD9VVx1lDrzHl8gUJNzdV3ch5rL10LgsEN9Mfueoe5LS0Yt23Xzm ObuQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=7hEAib8ELxLbUK8nJSpAmW9igi7majswKHW1Qhm97zw=; b=Tsrw+jIy4xSiacfJyKBEctRx+8/L+zmLNLyGjI9fF0N0+rNDGmtpEzMsoJjhuNTbRC 3rJ1HEgf6OTpUdq2Ehd88Yyzprt9zfdtwZ2mRyom1MZBGlePlSGf4YnK5Z4gOeThi7BQ hCQxeizK6wtrMiHFolIa9Um0D88hvg5N7HtMcZXGpuGBcJX3Eqs341JGnW8PkVNbLKmL K3kJQjKYKHm+qmeyYixGntqzZMTifUMimskcfKL/iQPqJ+V+5dQRV+d2/icHUUl0SpM0 sv7saxBFLgGeWC+YLvgCaK0/SCIhuQmTh3rRr4lnpxH5lDmQXCcf9JU2U6C/PtbuIgm3 67PQ== X-Gm-Message-State: APjAAAVgBXSDR7vsCUC5vuuNnGdlfiFjiKJc+8rPCsWBfcuyPGJYKNye xn+1zprapQDn2dt0yJQdN+ABfCWU4vU= X-Google-Smtp-Source: APXvYqwd7AhJfs74E/4H4vfEh1vfqrTxa3pK0D6AZmdK0JMBfMOh6tjgofwQ0jVHr86RUpw20487jg== X-Received: by 2002:a17:90a:fe02:: with SMTP id ck2mr1887438pjb.10.1581379254729; Mon, 10 Feb 2020 16:00:54 -0800 (PST) Received: from localhost ([2620:10d:c090:200::6168]) by smtp.gmail.com with ESMTPSA id r6sm1414846pfh.91.2020.02.10.16.00.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Feb 2020 16:00:54 -0800 (PST) From: Jes Sorensen X-Google-Original-From: Jes Sorensen To: linux-fscrypt@vger.kernel.org Cc: kernel-team@fb.com, Jes Sorensen Subject: [PATCH 2/7] Restructure fsverity_cmd_sign for shared libraries Date: Mon, 10 Feb 2020 19:00:32 -0500 Message-Id: <20200211000037.189180-3-Jes.Sorensen@gmail.com> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200211000037.189180-1-Jes.Sorensen@gmail.com> References: <20200211000037.189180-1-Jes.Sorensen@gmail.com> MIME-Version: 1.0 Sender: linux-fscrypt-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fscrypt@vger.kernel.org From: Jes Sorensen This moves the command line parsing, error reporting etc. to the command line tool, and turns fsverity_cmd_sign() into a call returning the digest and the signature. It is the responsibility of the caller to decide what to do with the returned objects. Signed-off-by: Jes Sorensen --- cmd_sign.c | 132 ++++++----------------------------------------------- commands.h | 23 +++++++++- fsverity.c | 129 ++++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 163 insertions(+), 121 deletions(-) diff --git a/cmd_sign.c b/cmd_sign.c index dcb37ce..2d3fa54 100644 --- a/cmd_sign.c +++ b/cmd_sign.c @@ -8,7 +8,6 @@ */ #include -#include #include #include #include @@ -38,19 +37,6 @@ struct fsverity_descriptor { __u8 signature[]; /* optional PKCS#7 signature */ }; -/* - * Format in which verity file measurements are signed. This is the same as - * 'struct fsverity_digest', except here some magic bytes are prepended to - * provide some context about what is being signed in case the same key is used - * for non-fsverity purposes, and here the fields have fixed endianness. - */ -struct fsverity_signed_digest { - char magic[8]; /* must be "FSVerity" */ - __le16 digest_algorithm; - __le16 digest_size; - __u8 digest[]; -}; - static void __printf(1, 2) __cold error_msg_openssl(const char *format, ...) { @@ -340,18 +326,6 @@ out: return ok; } -static bool write_signature(const char *filename, const u8 *sig, u32 sig_size) -{ - struct filedes file; - bool ok; - - if (!open_file(&file, filename, O_WRONLY|O_CREAT|O_TRUNC, 0644)) - return false; - ok = full_write(&file, sig, sig_size); - ok &= filedes_close(&file); - return ok; -} - #define FS_VERITY_MAX_LEVELS 64 struct block_buffer { @@ -507,93 +481,27 @@ out: return ok; } -enum { - OPT_HASH_ALG, - OPT_BLOCK_SIZE, - OPT_SALT, - OPT_KEY, - OPT_CERT, -}; - -static const struct option longopts[] = { - {"hash-alg", required_argument, NULL, OPT_HASH_ALG}, - {"block-size", required_argument, NULL, OPT_BLOCK_SIZE}, - {"salt", required_argument, NULL, OPT_SALT}, - {"key", required_argument, NULL, OPT_KEY}, - {"cert", required_argument, NULL, OPT_CERT}, - {NULL, 0, NULL, 0} -}; - /* Sign a file for fs-verity by computing its measurement, then signing it. */ -int fsverity_cmd_sign(const struct fsverity_command *cmd, - int argc, char *argv[]) +int fsverity_cmd_sign(char *filename, const struct fsverity_hash_alg *hash_alg, + u32 block_size, u8 *salt, u32 salt_size, + const char *keyfile, const char *certfile, + struct fsverity_signed_digest **retdigest, + u8 **sig, u32 *sig_size) { - const struct fsverity_hash_alg *hash_alg = NULL; - u32 block_size = 0; - u8 *salt = NULL; - u32 salt_size = 0; - const char *keyfile = NULL; - const char *certfile = NULL; struct fsverity_signed_digest *digest = NULL; - char digest_hex[FS_VERITY_MAX_DIGEST_SIZE * 2 + 1]; - u8 *sig = NULL; - u32 sig_size; int status; - int c; - - while ((c = getopt_long(argc, argv, "", longopts, NULL)) != -1) { - switch (c) { - case OPT_HASH_ALG: - if (hash_alg != NULL) { - error_msg("--hash-alg can only be specified once"); - goto out_usage; - } - hash_alg = find_hash_alg_by_name(optarg); - if (hash_alg == NULL) - goto out_usage; - break; - case OPT_BLOCK_SIZE: - if (!parse_block_size_option(optarg, &block_size)) - goto out_usage; - break; - case OPT_SALT: - if (!parse_salt_option(optarg, &salt, &salt_size)) - goto out_usage; - break; - case OPT_KEY: - if (keyfile != NULL) { - error_msg("--key can only be specified once"); - goto out_usage; - } - keyfile = optarg; - break; - case OPT_CERT: - if (certfile != NULL) { - error_msg("--cert can only be specified once"); - goto out_usage; - } - certfile = optarg; - break; - default: - goto out_usage; - } - } - argv += optind; - argc -= optind; - - if (argc != 2) - goto out_usage; - - if (hash_alg == NULL) - hash_alg = &fsverity_hash_algs[FS_VERITY_HASH_ALG_DEFAULT]; + if (hash_alg == NULL) { + status = -EINVAL; + goto out; + } if (block_size == 0) block_size = get_default_block_size(); if (keyfile == NULL) { - error_msg("Missing --key argument"); - goto out_usage; + status = -EINVAL; + goto out; } if (certfile == NULL) certfile = keyfile; @@ -603,33 +511,21 @@ int fsverity_cmd_sign(const struct fsverity_command *cmd, digest->digest_algorithm = cpu_to_le16(hash_alg - fsverity_hash_algs); digest->digest_size = cpu_to_le16(hash_alg->digest_size); - if (!compute_file_measurement(argv[0], hash_alg, block_size, + if (!compute_file_measurement(filename, hash_alg, block_size, salt, salt_size, digest->digest)) goto out_err; if (!sign_data(digest, sizeof(*digest) + hash_alg->digest_size, - keyfile, certfile, hash_alg, &sig, &sig_size)) - goto out_err; - - if (!write_signature(argv[1], sig, sig_size)) + keyfile, certfile, hash_alg, sig, sig_size)) goto out_err; - bin2hex(digest->digest, hash_alg->digest_size, digest_hex); - printf("Signed file '%s' (%s:%s)\n", argv[0], hash_alg->name, - digest_hex); + *retdigest = digest; status = 0; out: - free(salt); - free(digest); - free(sig); return status; out_err: status = 1; goto out; -out_usage: - usage(cmd, stderr); - status = 2; - goto out; } diff --git a/commands.h b/commands.h index 98f9745..c38fcea 100644 --- a/commands.h +++ b/commands.h @@ -5,17 +5,36 @@ #include #include "util.h" +#include "hash_algs.h" +#include "fsverity_uapi.h" struct fsverity_command; +/* + * Format in which verity file measurements are signed. This is the same as + * 'struct fsverity_digest', except here some magic bytes are prepended to + * provide some context about what is being signed in case the same key is used + * for non-fsverity purposes, and here the fields have fixed endianness. + */ +struct fsverity_signed_digest { + char magic[8]; /* must be "FSVerity" */ + __le16 digest_algorithm; + __le16 digest_size; + __u8 digest[]; +}; + + void usage(const struct fsverity_command *cmd, FILE *fp); int fsverity_cmd_enable(const struct fsverity_command *cmd, int argc, char *argv[]); int fsverity_cmd_measure(const struct fsverity_command *cmd, int argc, char *argv[]); -int fsverity_cmd_sign(const struct fsverity_command *cmd, - int argc, char *argv[]); +int fsverity_cmd_sign(char *filename, const struct fsverity_hash_alg *hash_alg, + u32 block_size, u8 *salt, u32 salt_size, + const char *keyfile, const char *certfile, + struct fsverity_signed_digest **retdigest, + u8 **sig, u32 *sig_size); bool parse_block_size_option(const char *arg, u32 *size_ptr); u32 get_default_block_size(void); diff --git a/fsverity.c b/fsverity.c index 9a44df1..6246031 100644 --- a/fsverity.c +++ b/fsverity.c @@ -7,14 +7,141 @@ * Written by Eric Biggers. */ +#include #include #include #include #include +#include +#include #include "commands.h" #include "hash_algs.h" +enum { + OPT_HASH_ALG, + OPT_BLOCK_SIZE, + OPT_SALT, + OPT_KEY, + OPT_CERT, +}; + +static const struct option longopts[] = { + {"hash-alg", required_argument, NULL, OPT_HASH_ALG}, + {"block-size", required_argument, NULL, OPT_BLOCK_SIZE}, + {"salt", required_argument, NULL, OPT_SALT}, + {"key", required_argument, NULL, OPT_KEY}, + {"cert", required_argument, NULL, OPT_CERT}, + {NULL, 0, NULL, 0} +}; + +static bool write_signature(const char *filename, const u8 *sig, u32 sig_size) +{ + struct filedes file; + bool ok; + + if (!open_file(&file, filename, O_WRONLY|O_CREAT|O_TRUNC, 0644)) + return false; + ok = full_write(&file, sig, sig_size); + ok &= filedes_close(&file); + return ok; +} + +int wrap_cmd_sign(const struct fsverity_command *cmd, int argc, char *argv[]) +{ + struct fsverity_signed_digest *digest = NULL; + u8 *sig = NULL; + u32 sig_size; + const struct fsverity_hash_alg *hash_alg = NULL; + u32 block_size = 0; + u8 *salt = NULL; + u32 salt_size = 0; + const char *keyfile = NULL; + const char *certfile = NULL; + int c, status; + char digest_hex[FS_VERITY_MAX_DIGEST_SIZE * 2 + 1]; + + while ((c = getopt_long(argc, argv, "", longopts, NULL)) != -1) { + switch (c) { + case OPT_HASH_ALG: + if (hash_alg != NULL) { + error_msg("--hash-alg can only be specified once"); + goto out_usage; + } + hash_alg = find_hash_alg_by_name(optarg); + if (hash_alg == NULL) + goto out_usage; + break; + case OPT_BLOCK_SIZE: + if (!parse_block_size_option(optarg, &block_size)) + goto out_usage; + break; + case OPT_SALT: + if (!parse_salt_option(optarg, &salt, &salt_size)) + goto out_usage; + break; + case OPT_KEY: + if (keyfile != NULL) { + error_msg("--key can only be specified once"); + goto out_usage; + } + keyfile = optarg; + break; + case OPT_CERT: + if (certfile != NULL) { + error_msg("--cert can only be specified once"); + goto out_usage; + } + certfile = optarg; + break; + default: + goto out_usage; + } + } + + if (keyfile == NULL) { + status = -EINVAL; + error_msg("Missing --key argument"); + goto out_usage; + } + + argv += optind; + argc -= optind; + + if (hash_alg == NULL) + hash_alg = &fsverity_hash_algs[FS_VERITY_HASH_ALG_DEFAULT]; + + if (argc != 2) + goto out_usage; + + status = fsverity_cmd_sign(argv[0], hash_alg, block_size, salt, salt_size, + keyfile, certfile, &digest, &sig, &sig_size); + if (status == -EINVAL) + goto out_usage; + if (status != 0) + goto out; + + if (!write_signature(argv[1], sig, sig_size)) { + status = -EIO; + goto out; + } + + bin2hex(digest->digest, hash_alg->digest_size, digest_hex); + printf("Signed file '%s' (%s:%s)\n", argv[0], hash_alg->name, + digest_hex); + + out: + free(salt); + free(digest); + free(sig); + return status; + + out_usage: + usage(cmd, stderr); + status = 2; + goto out; +} + static const struct fsverity_command { const char *name; int (*func)(const struct fsverity_command *cmd, int argc, char *argv[]); @@ -38,7 +165,7 @@ static const struct fsverity_command { " fsverity measure FILE...\n" }, { .name = "sign", - .func = fsverity_cmd_sign, + .func = wrap_cmd_sign, .short_desc = "Sign a file for fs-verity", .usage_str = " fsverity sign FILE OUT_SIGFILE --key=KEYFILE\n" From patchwork Tue Feb 11 00:00:33 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jes Sorensen X-Patchwork-Id: 11374245 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id D058F17F0 for ; Tue, 11 Feb 2020 00:01:00 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id A963B20733 for ; Tue, 11 Feb 2020 00:01:00 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="fwX8J/gc" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727530AbgBKABA (ORCPT ); Mon, 10 Feb 2020 19:01:00 -0500 Received: from mail-pf1-f196.google.com ([209.85.210.196]:35232 "EHLO mail-pf1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727490AbgBKABA (ORCPT ); Mon, 10 Feb 2020 19:01:00 -0500 Received: by mail-pf1-f196.google.com with SMTP id y73so4516472pfg.2 for ; Mon, 10 Feb 2020 16:01:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=ySm+v7oQp4kUguIPka/Yy8LzDkudWQuhqEaz1/vGM/Y=; b=fwX8J/gcFXjwZ957WFxpTJsz+6O8CsI8BMWE2A6fiDA9Wi9X4HQmDk0ha8rO1ZUgM6 +PIOVVD3QO1QpodyXmmczQ6KzoohVVa4emOL4NKKyKNFUL/yvp+2FPqU4X65OacUCuhV zy+RWYwDscdFdEl8qYTH6s3AWfUfiSB+pq1i94cbvOVBjDZ+hRtza7GIqPcFmcawLb3v n/r/GYOA0KiV2wlKh/L8ATGb5B0rgJwMhRDRzWxTOre5QgK3ac/NmSGKvzs8he9WY0vB f2q0kAwc7JVdbGmQ3VX3hh2eB7XE0bJKdwJhuGlk1bn4jc/3+ihHwg73D59wa565vmOH wo7w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=ySm+v7oQp4kUguIPka/Yy8LzDkudWQuhqEaz1/vGM/Y=; b=E2WJPcRgX63xkueKTc3QCl7dbff6mGsc/tjCoD5w0Nk5NAcPGvyjcnJM6n4qkzfGLg 4AxBglYTWH2kExESuc5zl8INFC8WsFWZwB8V15Ew+9I1BT60j4u/1nVP9RS5fFos8gKe e/2ppraMKJiHrYDof+uBeV9FT5jFAofvLpeqLxkMiwhOmInYZ+UkSZK2LqctvMeKikCu sWst6eYXGwA4hNE8OVP5BXV81UeqtRf5JJILtOJM6MAxi7MokzqUepEtnW43lcnfhiui Abcuu8Ks/lFZ0YQzo0iCO6nhBAQgRUqcfb0YInkWHGbQatK89YzXYtuHY4sXzslz0w2I RFAQ== X-Gm-Message-State: APjAAAUggj5hE1AM9dSOJ9MjppZWAjjGELYek9mnPjb60v1siMmmKps0 eTDXBKfmWQdE/r76uTsdZGXQMT3fX00= X-Google-Smtp-Source: APXvYqz2UWIVubqKTNFFhJHcQTmF8fzac3e7YikiwzBJgam5KoHPAG0W7Za3+1ovUTitXAl98e6w3g== X-Received: by 2002:a63:a1e:: with SMTP id 30mr4353601pgk.238.1581379259215; Mon, 10 Feb 2020 16:00:59 -0800 (PST) Received: from localhost ([2620:10d:c090:200::6168]) by smtp.gmail.com with ESMTPSA id i190sm1217517pgd.75.2020.02.10.16.00.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Feb 2020 16:00:58 -0800 (PST) From: Jes Sorensen X-Google-Original-From: Jes Sorensen To: linux-fscrypt@vger.kernel.org Cc: kernel-team@fb.com, Jes Sorensen Subject: [PATCH 3/7] Make fsverity_cmd_measure() a library function Date: Mon, 10 Feb 2020 19:00:33 -0500 Message-Id: <20200211000037.189180-4-Jes.Sorensen@gmail.com> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200211000037.189180-1-Jes.Sorensen@gmail.com> References: <20200211000037.189180-1-Jes.Sorensen@gmail.com> MIME-Version: 1.0 Sender: linux-fscrypt-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fscrypt@vger.kernel.org From: Jes Sorensen This splits the cmdline option parsing into wrap_cmd_measure() and fsverity_cmd_measure() is just the basic call to the ioctl. Signed-off-by: Jes Sorensen --- cmd_measure.c | 49 +++++++++---------------------------------------- commands.h | 3 +-- fsverity.c | 50 +++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 59 insertions(+), 43 deletions(-) diff --git a/cmd_measure.c b/cmd_measure.c index 574e3ca..fc3108d 100644 --- a/cmd_measure.c +++ b/cmd_measure.c @@ -13,50 +13,24 @@ #include "commands.h" #include "fsverity_uapi.h" -#include "hash_algs.h" /* Display the measurement of the given verity file(s). */ -int fsverity_cmd_measure(const struct fsverity_command *cmd, - int argc, char *argv[]) +int fsverity_cmd_measure(char *filename, struct fsverity_digest *d) { - struct fsverity_digest *d = NULL; struct filedes file; - char digest_hex[FS_VERITY_MAX_DIGEST_SIZE * 2 + 1]; - const struct fsverity_hash_alg *hash_alg; - char _hash_alg_name[32]; - const char *hash_alg_name; int status; - int i; - if (argc < 2) - goto out_usage; + if (!open_file(&file, filename, O_RDONLY, 0)) + goto out_err; - d = xzalloc(sizeof(*d) + FS_VERITY_MAX_DIGEST_SIZE); - - for (i = 1; i < argc; i++) { - d->digest_size = FS_VERITY_MAX_DIGEST_SIZE; - - if (!open_file(&file, argv[i], O_RDONLY, 0)) - goto out_err; - if (ioctl(file.fd, FS_IOC_MEASURE_VERITY, d) != 0) { - error_msg_errno("FS_IOC_MEASURE_VERITY failed on '%s'", - file.name); - filedes_close(&file); - goto out_err; - } + if (ioctl(file.fd, FS_IOC_MEASURE_VERITY, d) != 0) { + error_msg_errno("FS_IOC_MEASURE_VERITY failed on '%s'", + file.name); filedes_close(&file); - - ASSERT(d->digest_size <= FS_VERITY_MAX_DIGEST_SIZE); - bin2hex(d->digest, d->digest_size, digest_hex); - hash_alg = find_hash_alg_by_num(d->digest_algorithm); - if (hash_alg) { - hash_alg_name = hash_alg->name; - } else { - sprintf(_hash_alg_name, "ALG_%u", d->digest_algorithm); - hash_alg_name = _hash_alg_name; - } - printf("%s:%s %s\n", hash_alg_name, digest_hex, argv[i]); + goto out_err; } + filedes_close(&file); + status = 0; out: free(d); @@ -65,9 +39,4 @@ out: out_err: status = 1; goto out; - -out_usage: - usage(cmd, stderr); - status = 2; - goto out; } diff --git a/commands.h b/commands.h index c38fcea..3e07f3d 100644 --- a/commands.h +++ b/commands.h @@ -28,8 +28,7 @@ void usage(const struct fsverity_command *cmd, FILE *fp); int fsverity_cmd_enable(const struct fsverity_command *cmd, int argc, char *argv[]); -int fsverity_cmd_measure(const struct fsverity_command *cmd, - int argc, char *argv[]); +int fsverity_cmd_measure(char *filename, struct fsverity_digest *d); int fsverity_cmd_sign(char *filename, const struct fsverity_hash_alg *hash_alg, u32 block_size, u8 *salt, u32 salt_size, const char *keyfile, const char *certfile, diff --git a/fsverity.c b/fsverity.c index 6246031..49eca14 100644 --- a/fsverity.c +++ b/fsverity.c @@ -142,6 +142,54 @@ int wrap_cmd_sign(const struct fsverity_command *cmd, int argc, char *argv[]) goto out; } +int wrap_cmd_measure(const struct fsverity_command *cmd, + int argc, char *argv[]) +{ + struct fsverity_digest *d = NULL; + char digest_hex[FS_VERITY_MAX_DIGEST_SIZE * 2 + 1]; + const struct fsverity_hash_alg *hash_alg; + char _hash_alg_name[32]; + const char *hash_alg_name; + int status; + int i; + + if (argc < 2) + goto out_usage; + + d = xzalloc(sizeof(*d) + FS_VERITY_MAX_DIGEST_SIZE); + + for (i = 1; i < argc; i++) { + d->digest_size = FS_VERITY_MAX_DIGEST_SIZE; + + status = fsverity_cmd_measure(argv[i], d); + if (status) + goto out_err; + + ASSERT(d->digest_size <= FS_VERITY_MAX_DIGEST_SIZE); + bin2hex(d->digest, d->digest_size, digest_hex); + hash_alg = find_hash_alg_by_num(d->digest_algorithm); + if (hash_alg) { + hash_alg_name = hash_alg->name; + } else { + sprintf(_hash_alg_name, "ALG_%u", d->digest_algorithm); + hash_alg_name = _hash_alg_name; + } + printf("%s:%s %s\n", hash_alg_name, digest_hex, argv[i]); + } +out: + free(d); + return status; + +out_err: + status = 1; + goto out; + +out_usage: + usage(cmd, stderr); + status = 2; + goto out; +} + static const struct fsverity_command { const char *name; int (*func)(const struct fsverity_command *cmd, int argc, char *argv[]); @@ -158,7 +206,7 @@ static const struct fsverity_command { " [--signature=SIGFILE]\n" }, { .name = "measure", - .func = fsverity_cmd_measure, + .func = wrap_cmd_measure, .short_desc = "Display the measurement of the given verity file(s)", .usage_str = From patchwork Tue Feb 11 00:00:34 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jes Sorensen X-Patchwork-Id: 11374247 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 6A333921 for ; Tue, 11 Feb 2020 00:01:05 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 42F8920733 for ; Tue, 11 Feb 2020 00:01:05 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="DOe3U8tb" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727530AbgBKABF (ORCPT ); Mon, 10 Feb 2020 19:01:05 -0500 Received: from mail-pg1-f196.google.com ([209.85.215.196]:44164 "EHLO mail-pg1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727490AbgBKABE (ORCPT ); Mon, 10 Feb 2020 19:01:04 -0500 Received: by mail-pg1-f196.google.com with SMTP id g3so4729587pgs.11 for ; Mon, 10 Feb 2020 16:01:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=zt2NAcTQdEecK9A1hvj/zrK0BwYTai/7XWAy7cHFWkQ=; b=DOe3U8tb4rcSbd9YR96ps7iyShWDaWO4tTsD05Aqvwz5OYrfcceuId25Xu81snFUiV blErD2Jnh/YmlvRZBMeTEjqFEmewmXNBot8g2/rxC+YRBfUSuNeBZhyTR7CXnCvisUg/ Pm+zGWyf6jiVlVeggzacKTAV3pNqzksTzXIf0738sLLQ1nwCilmLpFrqL+ztM/gavSmU NsPkBMrCrjkBeukoleJ2exdgK9MB5N6d33spS355TGwKBwhqQ+klYpOJQG9oWXMFJIes uUVux72gJoM8e9FbMXn/OecnwtsFJz5rdmDc9B6FfzGHxJxf5bf1iISSMgehTI/L2kTF dSxA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=zt2NAcTQdEecK9A1hvj/zrK0BwYTai/7XWAy7cHFWkQ=; b=aRkCpKHRrd64WyghZMyVfBczxr0Q7wWb8ze4oudDx/MmcjaFH+Bm24kgzkgCp2LA70 89qK83JpMEW9UPT6wGt00CAVLZ2OBj4lLCrj5r8Rg3InhKYo2dfXvi8Q8Yu/v3iaoj1n uRswKKBFckHteBABtBOAMkorJtgZ+PUpaUH4ybOOBNPbt2olFzT6IJCEq9YD30QbpBI3 Wc3ljFf1Ce4s500cVTH/NOkAErJoN/B+iOI2UJw8pEX94yIYuG5bhOQI7KDh7zrDGjRA Sn1odG0Vx6PPMX0Xdqe/1m9yyV0DwT/1X4m5oXGTPo+lHnc19KHxpFI7alhHV2PUrHce SHww== X-Gm-Message-State: APjAAAWZt5dexA9rlHYLFbBXvqcXdl3zuBIh5GCdZG7beYKZOI/L5QLv BMeYcvuX3ir0Ph3btSUtUPYEUDuD8Do= X-Google-Smtp-Source: APXvYqy+Qn/+0n0Q7Tp8+NUQLjmcaK1pGZptBqvyLNez1cG7bxzRrDghxx2ZHtBiEQHMs5qhqqPNVQ== X-Received: by 2002:a63:4525:: with SMTP id s37mr4042448pga.418.1581379263634; Mon, 10 Feb 2020 16:01:03 -0800 (PST) Received: from localhost ([2620:10d:c090:200::6168]) by smtp.gmail.com with ESMTPSA id d14sm540030pjz.12.2020.02.10.16.01.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Feb 2020 16:01:03 -0800 (PST) From: Jes Sorensen X-Google-Original-From: Jes Sorensen To: linux-fscrypt@vger.kernel.org Cc: kernel-team@fb.com, Jes Sorensen Subject: [PATCH 4/7] Make fsverity_cmd_enable a library call() Date: Mon, 10 Feb 2020 19:00:34 -0500 Message-Id: <20200211000037.189180-5-Jes.Sorensen@gmail.com> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200211000037.189180-1-Jes.Sorensen@gmail.com> References: <20200211000037.189180-1-Jes.Sorensen@gmail.com> MIME-Version: 1.0 Sender: linux-fscrypt-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fscrypt@vger.kernel.org From: Jes Sorensen Split the parsing of command line arguments from the actual cmd call, which allows this to be called by other users. Signed-off-by: Jes Sorensen --- cmd_enable.c | 131 ++---------------------------------------------- commands.h | 3 +- fsverity.c | 137 ++++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 140 insertions(+), 131 deletions(-) diff --git a/cmd_enable.c b/cmd_enable.c index 1646299..8c55722 100644 --- a/cmd_enable.c +++ b/cmd_enable.c @@ -18,137 +18,17 @@ #include "fsverity_uapi.h" #include "hash_algs.h" -static bool parse_hash_alg_option(const char *arg, u32 *alg_ptr) -{ - char *end; - unsigned long n = strtoul(arg, &end, 10); - const struct fsverity_hash_alg *alg; - - if (*alg_ptr != 0) { - error_msg("--hash-alg can only be specified once"); - return false; - } - - /* Specified by number? */ - if (n > 0 && n < INT32_MAX && *end == '\0') { - *alg_ptr = n; - return true; - } - - /* Specified by name? */ - alg = find_hash_alg_by_name(arg); - if (alg != NULL) { - *alg_ptr = alg - fsverity_hash_algs; - return true; - } - return false; -} - -static bool read_signature(const char *filename, u8 **sig_ret, - u32 *sig_size_ret) -{ - struct filedes file = { .fd = -1 }; - u64 file_size; - u8 *sig = NULL; - bool ok = false; - - if (!open_file(&file, filename, O_RDONLY, 0)) - goto out; - if (!get_file_size(&file, &file_size)) - goto out; - if (file_size <= 0) { - error_msg("signature file '%s' is empty", filename); - goto out; - } - if (file_size > 1000000) { - error_msg("signature file '%s' is too large", filename); - goto out; - } - sig = xmalloc(file_size); - if (!full_read(&file, sig, file_size)) - goto out; - *sig_ret = sig; - *sig_size_ret = file_size; - sig = NULL; - ok = true; -out: - filedes_close(&file); - free(sig); - return ok; -} - -enum { - OPT_HASH_ALG, - OPT_BLOCK_SIZE, - OPT_SALT, - OPT_SIGNATURE, -}; - -static const struct option longopts[] = { - {"hash-alg", required_argument, NULL, OPT_HASH_ALG}, - {"block-size", required_argument, NULL, OPT_BLOCK_SIZE}, - {"salt", required_argument, NULL, OPT_SALT}, - {"signature", required_argument, NULL, OPT_SIGNATURE}, - {NULL, 0, NULL, 0} -}; - /* Enable fs-verity on a file. */ -int fsverity_cmd_enable(const struct fsverity_command *cmd, - int argc, char *argv[]) +int fsverity_cmd_enable(char *filename, struct fsverity_enable_arg *arg) { - struct fsverity_enable_arg arg = { .version = 1 }; u8 *salt = NULL; u8 *sig = NULL; struct filedes file; int status; - int c; - - while ((c = getopt_long(argc, argv, "", longopts, NULL)) != -1) { - switch (c) { - case OPT_HASH_ALG: - if (!parse_hash_alg_option(optarg, &arg.hash_algorithm)) - goto out_usage; - break; - case OPT_BLOCK_SIZE: - if (!parse_block_size_option(optarg, &arg.block_size)) - goto out_usage; - break; - case OPT_SALT: - if (!parse_salt_option(optarg, &salt, &arg.salt_size)) - goto out_usage; - arg.salt_ptr = (uintptr_t)salt; - break; - case OPT_SIGNATURE: - if (sig != NULL) { - error_msg("--signature can only be specified once"); - goto out_usage; - } - if (!read_signature(optarg, &sig, &arg.sig_size)) - goto out_err; - arg.sig_ptr = (uintptr_t)sig; - break; - default: - goto out_usage; - } - } - argv += optind; - argc -= optind; - - if (argc != 1) - goto out_usage; - - if (arg.hash_algorithm == 0) - arg.hash_algorithm = FS_VERITY_HASH_ALG_DEFAULT; - - if (arg.block_size == 0) - arg.block_size = get_default_block_size(); - - if (!open_file(&file, argv[0], O_RDONLY, 0)) + if (!open_file(&file, filename, O_RDONLY, 0)) goto out_err; - if (ioctl(file.fd, FS_IOC_ENABLE_VERITY, &arg) != 0) { - error_msg_errno("FS_IOC_ENABLE_VERITY failed on '%s'", - file.name); + if (ioctl(file.fd, FS_IOC_ENABLE_VERITY, arg) != 0) { filedes_close(&file); goto out_err; } @@ -164,9 +44,4 @@ out: out_err: status = 1; goto out; - -out_usage: - usage(cmd, stderr); - status = 2; - goto out; } diff --git a/commands.h b/commands.h index 3e07f3d..e490c25 100644 --- a/commands.h +++ b/commands.h @@ -26,8 +26,7 @@ struct fsverity_signed_digest { void usage(const struct fsverity_command *cmd, FILE *fp); -int fsverity_cmd_enable(const struct fsverity_command *cmd, - int argc, char *argv[]); +int fsverity_cmd_enable(char *filename, struct fsverity_enable_arg *arg); int fsverity_cmd_measure(char *filename, struct fsverity_digest *d); int fsverity_cmd_sign(char *filename, const struct fsverity_hash_alg *hash_alg, u32 block_size, u8 *salt, u32 salt_size, diff --git a/fsverity.c b/fsverity.c index 49eca14..b4e67a2 100644 --- a/fsverity.c +++ b/fsverity.c @@ -24,6 +24,7 @@ enum { OPT_SALT, OPT_KEY, OPT_CERT, + OPT_SIGNATURE, }; static const struct option longopts[] = { @@ -35,6 +36,14 @@ static const struct option longopts[] = { {NULL, 0, NULL, 0} }; +static const struct option enable_longopts[] = { + {"hash-alg", required_argument, NULL, OPT_HASH_ALG}, + {"block-size", required_argument, NULL, OPT_BLOCK_SIZE}, + {"salt", required_argument, NULL, OPT_SALT}, + {"signature", required_argument, NULL, OPT_SIGNATURE}, + {NULL, 0, NULL, 0} +}; + static bool write_signature(const char *filename, const u8 *sig, u32 sig_size) { struct filedes file; @@ -47,6 +56,65 @@ static bool write_signature(const char *filename, const u8 *sig, u32 sig_size) return ok; } +static bool read_signature(const char *filename, u8 **sig_ret, + u32 *sig_size_ret) +{ + struct filedes file = { .fd = -1 }; + u64 file_size; + u8 *sig = NULL; + bool ok = false; + + if (!open_file(&file, filename, O_RDONLY, 0)) + goto out; + if (!get_file_size(&file, &file_size)) + goto out; + if (file_size <= 0) { + error_msg("signature file '%s' is empty", filename); + goto out; + } + if (file_size > 1000000) { + error_msg("signature file '%s' is too large", filename); + goto out; + } + sig = xmalloc(file_size); + if (!full_read(&file, sig, file_size)) + goto out; + *sig_ret = sig; + *sig_size_ret = file_size; + sig = NULL; + ok = true; +out: + filedes_close(&file); + free(sig); + return ok; +} + +static bool parse_hash_alg_option(const char *arg, u32 *alg_ptr) +{ + char *end; + unsigned long n = strtoul(arg, &end, 10); + const struct fsverity_hash_alg *alg; + + if (*alg_ptr != 0) { + error_msg("--hash-alg can only be specified once"); + return false; + } + + /* Specified by number? */ + if (n > 0 && n < INT32_MAX && *end == '\0') { + *alg_ptr = n; + return true; + } + + /* Specified by name? */ + alg = find_hash_alg_by_name(arg); + if (alg != NULL) { + *alg_ptr = alg - fsverity_hash_algs; + return true; + } + return false; +} + int wrap_cmd_sign(const struct fsverity_command *cmd, int argc, char *argv[]) { struct fsverity_signed_digest *digest = NULL; @@ -190,6 +258,73 @@ out_usage: goto out; } +int wrap_cmd_enable(const struct fsverity_command *cmd, + int argc, char *argv[]) +{ + struct fsverity_enable_arg arg = { .version = 1 }; + u8 *salt = NULL; + u8 *sig = NULL; + int status; + int c; + + while ((c = getopt_long(argc, argv, "", enable_longopts, NULL)) != -1) { + switch (c) { + case OPT_HASH_ALG: + if (!parse_hash_alg_option(optarg, &arg.hash_algorithm)) + goto out_usage; + break; + case OPT_BLOCK_SIZE: + if (!parse_block_size_option(optarg, &arg.block_size)) + goto out_usage; + break; + case OPT_SALT: + if (!parse_salt_option(optarg, &salt, &arg.salt_size)) + goto out_usage; + arg.salt_ptr = (uintptr_t)salt; + break; + case OPT_SIGNATURE: + if (sig != NULL) { + error_msg("--signature can only be specified once"); + goto out_usage; + } + if (!read_signature(optarg, &sig, &arg.sig_size)) { + error_msg("unable to read signature file %s", + optarg); + status = 1; + goto out; + } + arg.sig_ptr = (uintptr_t)sig; + break; + default: + goto out_usage; + } + } + + argv += optind; + argc -= optind; + + if (argc != 1) + goto out_usage; + + if (arg.hash_algorithm == 0) + arg.hash_algorithm = FS_VERITY_HASH_ALG_DEFAULT; + + if (arg.block_size == 0) + arg.block_size = get_default_block_size(); + + status = fsverity_cmd_enable(argv[0], &arg); + +out: + free(salt); + free(sig); + return status; + +out_usage: + usage(cmd, stderr); + status = 2; + goto out; +} + static const struct fsverity_command { const char *name; int (*func)(const struct fsverity_command *cmd, int argc, char *argv[]); @@ -198,7 +333,7 @@ static const struct fsverity_command { } fsverity_commands[] = { { .name = "enable", - .func = fsverity_cmd_enable, + .func = wrap_cmd_enable, .short_desc = "Enable fs-verity on a file", .usage_str = " fsverity enable FILE\n" From patchwork Tue Feb 11 00:00:35 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jes Sorensen X-Patchwork-Id: 11374249 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id EC87117F0 for ; Tue, 11 Feb 2020 00:01:09 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id CDF8620733 for ; Tue, 11 Feb 2020 00:01:09 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="sgeuALDW" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727496AbgBKABJ (ORCPT ); Mon, 10 Feb 2020 19:01:09 -0500 Received: from mail-pf1-f193.google.com ([209.85.210.193]:33883 "EHLO mail-pf1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727490AbgBKABJ (ORCPT ); Mon, 10 Feb 2020 19:01:09 -0500 Received: by mail-pf1-f193.google.com with SMTP id i6so4521286pfc.1 for ; Mon, 10 Feb 2020 16:01:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=54BSlm+xY77Fsoq/IY3AfoejUu+eGYmqNFBZz8JILFE=; b=sgeuALDWDoE5eglUOlZqWoc6Jy3ZTz95pVumxMPXX5eZhBoEfRdZF27COvsu/o4rMA kf5I4UKVPJPslW/WadsJMqFMzxbV2ZKSw/KbiSrN0sS96EEy6sLf0myzhPY4mOJUNz+f tbpe1f/trTllY/sjaIe+9YklAQKK5JS4qGErLoKSMVqNT1zIg+HMn11GmGamMz761h8u 4XFPStLNHtTPdhL7ICUT7A3Bi97QNQpe/hQL2dWaiTR1ifiYCsW+gfVuhh8L/OYk68Gn rjrd0sgpjH10HfWqky0HhIcHRCygcENIhd77A8FXraghqxXi3YLzFJw5d1kQTcmc/Dmm i6ZQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=54BSlm+xY77Fsoq/IY3AfoejUu+eGYmqNFBZz8JILFE=; b=JkhjiK6uo7+S+PrqtpJpZUrcF1uSlvErMGrF8MHuhXNRI9AKKH48V1rt8umJ3BxSi2 KdLPVKA0vUt19dCwDW0EH5h3b2wcGsyR7pqD1BEegKsGjoFj5Lm3TdQJKJzU8YDPGyBk Ew/xqCDI81aiMKp6+QSNVVXmQ9KB08KLdFzB7swn3f1rFQh4pHdNsGuFbP/ftrfst002 gZCLpJ6XRRXVVVyzhtK5zJCLrhbM1E8FWUFbS3lqaByRtE2s0KC/uqMufrWLBPQLCWRG I7EWJw+psOS4ThMtuXkwOQk+Ak2XPgJVVRjLrbZOABvuZlpbA7NBJv8QpiADafqszvQT JuIA== X-Gm-Message-State: APjAAAVgWEOyR5U9YK9TGw6MjFRwC8cNEQs8FXWX+JTm/Vw0cctOUBdC aIISaCKtxhWELJtYtPizrSSp+mDx+Lw= X-Google-Smtp-Source: APXvYqwhucg7xQdxsKwl03ma/hgwdju6QB62sxWcB1ZxeAVnHwXZkNlTnvDg781sNVEP0Ex+dWvhWg== X-Received: by 2002:a65:5ccc:: with SMTP id b12mr4078625pgt.124.1581379268478; Mon, 10 Feb 2020 16:01:08 -0800 (PST) Received: from localhost ([2620:10d:c090:200::6168]) by smtp.gmail.com with ESMTPSA id a9sm1541482pfo.35.2020.02.10.16.01.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Feb 2020 16:01:07 -0800 (PST) From: Jes Sorensen X-Google-Original-From: Jes Sorensen To: linux-fscrypt@vger.kernel.org Cc: kernel-team@fb.com, Jes Sorensen Subject: [PATCH 5/7] Rename commands.h to fsverity.h Date: Mon, 10 Feb 2020 19:00:35 -0500 Message-Id: <20200211000037.189180-6-Jes.Sorensen@gmail.com> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200211000037.189180-1-Jes.Sorensen@gmail.com> References: <20200211000037.189180-1-Jes.Sorensen@gmail.com> MIME-Version: 1.0 Sender: linux-fscrypt-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fscrypt@vger.kernel.org From: Jes Sorensen This is a more appropriate name to provide the API for the shared library Signed-off-by: Jes Sorensen --- cmd_enable.c | 2 +- cmd_measure.c | 2 +- cmd_sign.c | 2 +- fsverity.c | 2 +- commands.h => fsverity.h | 0 5 files changed, 4 insertions(+), 4 deletions(-) rename commands.h => fsverity.h (100%) diff --git a/cmd_enable.c b/cmd_enable.c index 8c55722..732f3f6 100644 --- a/cmd_enable.c +++ b/cmd_enable.c @@ -14,7 +14,7 @@ #include #include -#include "commands.h" +#include "fsverity.h" #include "fsverity_uapi.h" #include "hash_algs.h" diff --git a/cmd_measure.c b/cmd_measure.c index fc3108d..3cd313e 100644 --- a/cmd_measure.c +++ b/cmd_measure.c @@ -11,7 +11,7 @@ #include #include -#include "commands.h" +#include "fsverity.h" #include "fsverity_uapi.h" /* Display the measurement of the given verity file(s). */ diff --git a/cmd_sign.c b/cmd_sign.c index 2d3fa54..42779f2 100644 --- a/cmd_sign.c +++ b/cmd_sign.c @@ -16,7 +16,7 @@ #include #include -#include "commands.h" +#include "fsverity.h" #include "fsverity_uapi.h" #include "hash_algs.h" diff --git a/fsverity.c b/fsverity.c index b4e67a2..f0e94bf 100644 --- a/fsverity.c +++ b/fsverity.c @@ -15,7 +15,7 @@ #include #include -#include "commands.h" +#include "fsverity.h" #include "hash_algs.h" enum { diff --git a/commands.h b/fsverity.h similarity index 100% rename from commands.h rename to fsverity.h From patchwork Tue Feb 11 00:00:36 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jes Sorensen X-Patchwork-Id: 11374251 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id A690717F0 for ; Tue, 11 Feb 2020 00:01:15 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 8781E20733 for ; Tue, 11 Feb 2020 00:01:15 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="YNN8LD5b" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727584AbgBKABP (ORCPT ); Mon, 10 Feb 2020 19:01:15 -0500 Received: from mail-pl1-f196.google.com ([209.85.214.196]:44255 "EHLO mail-pl1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727549AbgBKABP (ORCPT ); Mon, 10 Feb 2020 19:01:15 -0500 Received: by mail-pl1-f196.google.com with SMTP id d9so3467916plo.11 for ; Mon, 10 Feb 2020 16:01:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=9u2/hYYLGoO8El+py7ZmKj3n0T2jkISIzfeNreQh7Jg=; b=YNN8LD5bWbrsZh0CvgSzUsWM3sQsPSPpPW+jIxETDNcGAehbWznLtNP8LigrZiO0Cs xqCCew+E051S7lua1AjMItMrYfr9EAdldB3RLdVE+9YIhPbqzkhzxCVAIuYNADggDOBH h4O2vpM3oTp+f+VwZbqIghg1JvAG0ULybFtNIJWp7oVtZegki0fbOz2gN3YtYVO7kOLr 2i/G+N4AmF6OqUVoebyvVIQe2tg+bG0Us8eda8hNJjzzjXQ5UaTfo86pah2dTB1WAZlO zvdecd8CSPNRK+hLKZIoWXw2tOzh0V7uyV1WFetOxMtxYjNdmbySPXK6/+Mss77cteuc 3mjA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=9u2/hYYLGoO8El+py7ZmKj3n0T2jkISIzfeNreQh7Jg=; b=aLliO2Lawwc4lOFg8s2v4uLZWoae+B0Qt+ZQyLpr9usQYrTTY+x3t04VHZlWLLn36R Xr/9MAgqHx9QJwr1zfOlsRMN2WPgRJCSEWXphMEbuw22Xuyv5SossaTDzEA/uZty+l0P CiTPMDmdl0yi02QfgjjaxXI0Y/bqf+sjLeVnBvJJxGi3J+G7bcihDMRz6AJww7FgAKIY RTqsVsYpJbT5w9q56HzxLig62yMNfVIxsJYgbRRxw5U1FwWSfdkS7dQmWI5ItBLROvuF mCzozwo65udOKkOGpK3eQJwt645fOkBffWakaCjN+GZEqjL2jLyEDdpEGgUJaomN7NE+ A6Ug== X-Gm-Message-State: APjAAAU2g/n7Nl0Ifo2EPmmyXQglO5Zuc5sdwZWlV0Ve0XyY4tbwk6Yx U9epo/xGZBeaP1C7Kgor/uQv08ze5T0= X-Google-Smtp-Source: APXvYqzf8X2g4iAZ1GR6M/H5NMkqvmGuH11TF68qb1xF3LaXEsnJxFRREyRT3nD1VVfQtFZR9umhJA== X-Received: by 2002:a17:90a:8806:: with SMTP id s6mr359191pjn.141.1581379273949; Mon, 10 Feb 2020 16:01:13 -0800 (PST) Received: from localhost ([2620:10d:c090:200::6168]) by smtp.gmail.com with ESMTPSA id r11sm1280782pgi.9.2020.02.10.16.01.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Feb 2020 16:01:13 -0800 (PST) From: Jes Sorensen X-Google-Original-From: Jes Sorensen To: linux-fscrypt@vger.kernel.org Cc: kernel-team@fb.com, Jes Sorensen Subject: [PATCH 6/7] Move cmdline helper functions to fsverity.c Date: Mon, 10 Feb 2020 19:00:36 -0500 Message-Id: <20200211000037.189180-7-Jes.Sorensen@gmail.com> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200211000037.189180-1-Jes.Sorensen@gmail.com> References: <20200211000037.189180-1-Jes.Sorensen@gmail.com> MIME-Version: 1.0 Sender: linux-fscrypt-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fscrypt@vger.kernel.org From: Jes Sorensen There is no need for these to live in the shared library. In addition move get_default_block_size() to the library and rename it appropriately. Signed-off-by: Jes Sorensen --- cmd_sign.c | 2 +- fsverity.c | 25 +++++++++---------------- fsverity.h | 8 +------- util.c | 13 +++++++++++++ 4 files changed, 24 insertions(+), 24 deletions(-) diff --git a/cmd_sign.c b/cmd_sign.c index 42779f2..a0bd168 100644 --- a/cmd_sign.c +++ b/cmd_sign.c @@ -497,7 +497,7 @@ int fsverity_cmd_sign(char *filename, const struct fsverity_hash_alg *hash_alg, } if (block_size == 0) - block_size = get_default_block_size(); + block_size = fsverity_get_default_block_size(); if (keyfile == NULL) { status = -EINVAL; diff --git a/fsverity.c b/fsverity.c index f0e94bf..45bf0cc 100644 --- a/fsverity.c +++ b/fsverity.c @@ -18,6 +18,12 @@ #include "fsverity.h" #include "hash_algs.h" +struct fsverity_command; + +static bool parse_block_size_option(const char *arg, u32 *size_ptr); +static bool parse_salt_option(const char *arg, u8 **salt_ptr, u32 *salt_size_ptr); +static void usage(const struct fsverity_command *cmd, FILE *fp); + enum { OPT_HASH_ALG, OPT_BLOCK_SIZE, @@ -310,7 +316,7 @@ int wrap_cmd_enable(const struct fsverity_command *cmd, arg.hash_algorithm = FS_VERITY_HASH_ALG_DEFAULT; if (arg.block_size == 0) - arg.block_size = get_default_block_size(); + arg.block_size = fsverity_get_default_block_size(); status = fsverity_cmd_enable(argv[0], &arg); @@ -437,7 +443,7 @@ static const struct fsverity_command *find_command(const char *name) return NULL; } -bool parse_block_size_option(const char *arg, u32 *size_ptr) +static bool parse_block_size_option(const char *arg, u32 *size_ptr) { char *end; unsigned long n = strtoul(arg, &end, 10); @@ -455,7 +461,7 @@ bool parse_block_size_option(const char *arg, u32 *size_ptr) return true; } -bool parse_salt_option(const char *arg, u8 **salt_ptr, u32 *salt_size_ptr) +static bool parse_salt_option(const char *arg, u8 **salt_ptr, u32 *salt_size_ptr) { if (*salt_ptr != NULL) { error_msg("--salt can only be specified once"); @@ -470,19 +476,6 @@ bool parse_salt_option(const char *arg, u8 **salt_ptr, u32 *salt_size_ptr) return true; } -u32 get_default_block_size(void) -{ - long n = sysconf(_SC_PAGESIZE); - - if (n <= 0 || n >= INT_MAX || !is_power_of_2(n)) { - fprintf(stderr, - "Warning: invalid _SC_PAGESIZE (%ld). Assuming 4K blocks.\n", - n); - return 4096; - } - return n; -} - int main(int argc, char *argv[]) { const struct fsverity_command *cmd; diff --git a/fsverity.h b/fsverity.h index e490c25..bb2f337 100644 --- a/fsverity.h +++ b/fsverity.h @@ -8,8 +8,6 @@ #include "hash_algs.h" #include "fsverity_uapi.h" -struct fsverity_command; - /* * Format in which verity file measurements are signed. This is the same as * 'struct fsverity_digest', except here some magic bytes are prepended to @@ -24,7 +22,7 @@ struct fsverity_signed_digest { }; -void usage(const struct fsverity_command *cmd, FILE *fp); +u32 fsverity_get_default_block_size(void); int fsverity_cmd_enable(char *filename, struct fsverity_enable_arg *arg); int fsverity_cmd_measure(char *filename, struct fsverity_digest *d); @@ -34,8 +32,4 @@ int fsverity_cmd_sign(char *filename, const struct fsverity_hash_alg *hash_alg, struct fsverity_signed_digest **retdigest, u8 **sig, u32 *sig_size); -bool parse_block_size_option(const char *arg, u32 *size_ptr); -u32 get_default_block_size(void); -bool parse_salt_option(const char *arg, u8 **salt_ptr, u32 *salt_size_ptr); - #endif /* COMMANDS_H */ diff --git a/util.c b/util.c index 2218f2e..e4ccd2a 100644 --- a/util.c +++ b/util.c @@ -213,3 +213,16 @@ void bin2hex(const u8 *bin, size_t bin_len, char *hex) } *hex = '\0'; } + +u32 fsverity_get_default_block_size(void) +{ + long n = sysconf(_SC_PAGESIZE); + + if (n <= 0 || n >= INT_MAX || !is_power_of_2(n)) { + fprintf(stderr, + "Warning: invalid _SC_PAGESIZE (%ld). Assuming 4K blocks.\n", + n); + return 4096; + } + return n; +} From patchwork Tue Feb 11 00:00:37 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jes Sorensen X-Patchwork-Id: 11374253 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 61F36921 for ; Tue, 11 Feb 2020 00:01:20 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 4394B2082F for ; Tue, 11 Feb 2020 00:01:20 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="ddjhdR70" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727549AbgBKABU (ORCPT ); Mon, 10 Feb 2020 19:01:20 -0500 Received: from mail-pf1-f194.google.com ([209.85.210.194]:44294 "EHLO mail-pf1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727490AbgBKABT (ORCPT ); Mon, 10 Feb 2020 19:01:19 -0500 Received: by mail-pf1-f194.google.com with SMTP id y5so4493347pfb.11 for ; Mon, 10 Feb 2020 16:01:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=giQvuKX7jiy8E1/i0YN5clCiLU0NvaHWkduEDD6DQx4=; b=ddjhdR70VN8J7D/JrTWIb4Bfpg5ZXAmb/f/u3qcOw15XvzoW6E2rEinw/ScgT67lQ+ V6ZOcOmlAGL/IyLPx7Kw7UBQCOTL6L12r4yJQ5uAycN+Twhm3Y0CkcffTMDPoXGx8Gct BTn8ZlnF9Z6vkSgzJl7A4a7jxdobEr3C8DneNFMkDZ1cPzmgx1kNKAJSA1ANpFFFgb6j zcA5u63r7xbT8lUVYh2DMEA+ZxqHApsS0u5OOdLksqq+oP78TpIEL52AyjiK+ifgBd29 CBNER99+WFxAOIilkTA3439aBOs5nCLslEzMg/L1jQqlxKo9Fj1/zTgu8CnWiVXvpGdT T6og== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=giQvuKX7jiy8E1/i0YN5clCiLU0NvaHWkduEDD6DQx4=; b=AzhWwZbE/03vWNuot8fi/6uPw5H/PfN3aGV7TUMnzcu8YSz5qa9ZQIlefMqMaJAPlk j5AjngE7hVcuMw4oSJu/l/ol1LYnn66xcDww/aDDdXTvSlJczX5DexexXcUUtv9Z9Bct Ca8sjQ8cotvGer4dlKPCy9KXWxsR+HJ6+upllRaYFRz2AxtaHYDcTxuR0HrMLwSgp32R TpMWxVCJ9II8Jth1KE9DbsiUg+2Yv6GeoVOTjVwTnq2xnRm7mbBdQq5iKPJ0z53/tuJm dKre7a5DBk4SZZ7PymXQFPqEQ7EzsnwVllSmU1j/sBf6eDkTDm6Sib6xLcjIXqCbXfQn +YiA== X-Gm-Message-State: APjAAAWuGLO7TKcB9f7bpMHeobe8cE+bQ//jqma97uUyjyI2VHH7ZRB+ Gi4QNLgIPz+ecypJpLUHOWQ6qyVhBTQ= X-Google-Smtp-Source: APXvYqxmOQUdcMMU+2O2/SeVGu6Kj14tr7s6By6yTJPlrOgetsF8CbZw7eRkz3J2hE7qOXE6Bq2d1w== X-Received: by 2002:a62:1c95:: with SMTP id c143mr326924pfc.219.1581379278564; Mon, 10 Feb 2020 16:01:18 -0800 (PST) Received: from localhost ([2620:10d:c090:200::6168]) by smtp.gmail.com with ESMTPSA id b18sm1527618pfd.63.2020.02.10.16.01.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Feb 2020 16:01:18 -0800 (PST) From: Jes Sorensen X-Google-Original-From: Jes Sorensen To: linux-fscrypt@vger.kernel.org Cc: kernel-team@fb.com, Jes Sorensen Subject: [PATCH 7/7] cmd_sign: fsverity_cmd_sign() into two functions Date: Mon, 10 Feb 2020 19:00:37 -0500 Message-Id: <20200211000037.189180-8-Jes.Sorensen@gmail.com> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200211000037.189180-1-Jes.Sorensen@gmail.com> References: <20200211000037.189180-1-Jes.Sorensen@gmail.com> MIME-Version: 1.0 Sender: linux-fscrypt-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fscrypt@vger.kernel.org From: Jes Sorensen This splits cmd_sign() into a gen_digest() and a sign_digest() function, and fixes fsverity.c to use them appropriately. --- cmd_sign.c | 50 +++++++++++++++++++++++++++++++++----------------- fsverity.c | 8 ++++++-- fsverity.h | 13 ++++++++----- 3 files changed, 47 insertions(+), 24 deletions(-) diff --git a/cmd_sign.c b/cmd_sign.c index a0bd168..ba68243 100644 --- a/cmd_sign.c +++ b/cmd_sign.c @@ -481,12 +481,11 @@ out: return ok; } -/* Sign a file for fs-verity by computing its measurement, then signing it. */ -int fsverity_cmd_sign(char *filename, const struct fsverity_hash_alg *hash_alg, - u32 block_size, u8 *salt, u32 salt_size, - const char *keyfile, const char *certfile, - struct fsverity_signed_digest **retdigest, - u8 **sig, u32 *sig_size) +/* Generate the fsverity digest computing its measurement. */ +int fsverity_cmd_gen_digest(char *filename, + const struct fsverity_hash_alg *hash_alg, + u32 block_size, u8 *salt, u32 salt_size, + struct fsverity_signed_digest **retdigest) { struct fsverity_signed_digest *digest = NULL; int status; @@ -499,13 +498,6 @@ int fsverity_cmd_sign(char *filename, const struct fsverity_hash_alg *hash_alg, if (block_size == 0) block_size = fsverity_get_default_block_size(); - if (keyfile == NULL) { - status = -EINVAL; - goto out; - } - if (certfile == NULL) - certfile = keyfile; - digest = xzalloc(sizeof(*digest) + hash_alg->digest_size); memcpy(digest->magic, "FSVerity", 8); digest->digest_algorithm = cpu_to_le16(hash_alg - fsverity_hash_algs); @@ -515,10 +507,6 @@ int fsverity_cmd_sign(char *filename, const struct fsverity_hash_alg *hash_alg, salt, salt_size, digest->digest)) goto out_err; - if (!sign_data(digest, sizeof(*digest) + hash_alg->digest_size, - keyfile, certfile, hash_alg, sig, sig_size)) - goto out_err; - *retdigest = digest; status = 0; out: @@ -529,3 +517,31 @@ out_err: goto out; } + +/* Sign a pre-generated fsverity_signed_digest structure */ +int fsverity_cmd_sign_digest(struct fsverity_signed_digest *digest, + const struct fsverity_hash_alg *hash_alg, + const char *keyfile, const char *certfile, + u8 **sig, u32 *sig_size) +{ + int status; + + if (keyfile == NULL) { + status = -EINVAL; + goto out; + } + if (certfile == NULL) + certfile = keyfile; + + if (!sign_data(digest, sizeof(*digest) + hash_alg->digest_size, + keyfile, certfile, hash_alg, sig, sig_size)) + goto out_err; + + status = 0; + out: + return status; + + out_err: + status = 1; + goto out; +} diff --git a/fsverity.c b/fsverity.c index 45bf0cc..3fcafcb 100644 --- a/fsverity.c +++ b/fsverity.c @@ -188,8 +188,12 @@ int wrap_cmd_sign(const struct fsverity_command *cmd, int argc, char *argv[]) if (argc != 2) goto out_usage; - status = fsverity_cmd_sign(argv[0], hash_alg, block_size, salt, salt_size, - keyfile, certfile, &digest, &sig, &sig_size); + status = fsverity_cmd_gen_digest(argv[0], hash_alg, block_size, + salt, salt_size, &digest); + if (status) + goto out_usage; + status = fsverity_cmd_sign_digest(digest, hash_alg, keyfile, certfile, + &sig, &sig_size); if (status == -EINVAL) goto out_usage; if (status != 0) diff --git a/fsverity.h b/fsverity.h index bb2f337..695bdac 100644 --- a/fsverity.h +++ b/fsverity.h @@ -26,10 +26,13 @@ u32 fsverity_get_default_block_size(void); int fsverity_cmd_enable(char *filename, struct fsverity_enable_arg *arg); int fsverity_cmd_measure(char *filename, struct fsverity_digest *d); -int fsverity_cmd_sign(char *filename, const struct fsverity_hash_alg *hash_alg, - u32 block_size, u8 *salt, u32 salt_size, - const char *keyfile, const char *certfile, - struct fsverity_signed_digest **retdigest, - u8 **sig, u32 *sig_size); +int fsverity_cmd_gen_digest(char *filename, + const struct fsverity_hash_alg *hash_alg, + u32 block_size, u8 *salt, u32 salt_size, + struct fsverity_signed_digest **retdigest); +int fsverity_cmd_sign_digest(struct fsverity_signed_digest *digest, + const struct fsverity_hash_alg *hash_alg, + const char *keyfile, const char *certfile, + u8 **sig, u32 *sig_size); #endif /* COMMANDS_H */