From patchwork Thu Aug 23 10:43:34 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jens Wiklander X-Patchwork-Id: 144921 Delivered-To: patches@linaro.org Received: by 2002:a2e:1648:0:0:0:0:0 with SMTP id 8-v6csp30738ljw; Thu, 23 Aug 2018 03:44:01 -0700 (PDT) X-Received: by 2002:a19:990d:: with SMTP id b13-v6mr20722169lfe.13.1535021041735; Thu, 23 Aug 2018 03:44:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1535021041; cv=none; d=google.com; s=arc-20160816; b=ViglQp193jEYdDQ65HPPpMD5VPum46k0PbzhNASwNoIsBOzacvmwNMGiMwYAKqZ9IS b3rMt/igXm78RMhfNcc5hi7h6onfBvfDIReTVfvNF1xXSuRxH/W6X/dUVERvCeD62GrG Los4brN2nZ0Wxl7F/5HploZPVBhNx7etE0UzU0AWNxxNCqRvsZqUwFJfLrlhbR+9XRsF kk9RpGxQ0qAHmYvhhZGhS1dL2RIkN8x9GzxIp6J19XWij21p4otl4bPhXJW8YM8DBHiB WL4uP4Rqda+cHQjgzf4U+i5eBa9dnNOeD/c/IJ0iSI25hgAzwitfqprJ8wQ2cOd1YOa3 /1uA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature:arc-authentication-results; bh=XI3RDZrNUKBCohm8WMnrspag5GNB06mvPA11+FTzPuA=; b=XKGCA0DdzkEXDSYyAWTBlnNbr5ODKm1PUdICPcFqgEIGOp7Dr6Qtp1L7WpiUSYjCPM 4BPjmRSeCT0hLy5T4umlp4LNLHRRSpNd+W3ERz0Ag1q5usYgrOrIx72OUOZxdCW5KSig ey7W30wN2+UpIjKbzIo2JvcINAZavop0qj3F8bzDs8gJIr/uH2X7wIdml1gd9XoQ1dpQ FaKRwHOz2XhfTuugwa6kutVixQWTl/df+agImDdeBmWEPVii7EbXWgoc7cy8WqnmOHcQ ERVDPlSWZFrSL0UNwIya+QUQyLy//eh0Uiqcd/rmLSfHfN2pmdOXN/+Hmv4/k1obKfm6 gvUg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=TQLgRXCQ; spf=pass (google.com: domain of jens.wiklander@linaro.org designates 209.85.220.41 as permitted sender) smtp.mailfrom=jens.wiklander@linaro.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from mail-sor-f41.google.com (mail-sor-f41.google.com. [209.85.220.41]) by mx.google.com with SMTPS id k1-v6sor475756lfb.172.2018.08.23.03.44.01 for (Google Transport Security); Thu, 23 Aug 2018 03:44:01 -0700 (PDT) Received-SPF: pass (google.com: domain of jens.wiklander@linaro.org designates 209.85.220.41 as permitted sender) client-ip=209.85.220.41; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=TQLgRXCQ; spf=pass (google.com: domain of jens.wiklander@linaro.org designates 209.85.220.41 as permitted sender) smtp.mailfrom=jens.wiklander@linaro.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=XI3RDZrNUKBCohm8WMnrspag5GNB06mvPA11+FTzPuA=; b=TQLgRXCQQSCNutVxCUD+kxJmh1MKs1mWpF4nDKzhAKXWZTIJ9MdheS0OP2aeOS3bZ+ XloJ0i3Wbkn/Od3tHCCtWq1wJkp00LdRPcEn8X7zhG+A8YUH7+POHrNYgCM8bNRWcTOP srwyCAN9rqOHqRbphOs8+/ODu2rCADO+0S5p0= 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; bh=XI3RDZrNUKBCohm8WMnrspag5GNB06mvPA11+FTzPuA=; b=a1YE4vI3P8tCltylwSJNHhAG4gR4ratRSW+hLbmaOmhQFFMWw6vU99eLKMhRvfMrgy 4Le/9ZAJBClNtRgK6zGeSEc/jOIAyrLBiOIT/3sOiXQF7On9NQgHNpMdtMC+y2jYaBln B6iKRgdCctxBS7qewiYtHOw8PuYLebYvuWl+9a+5fKwmC/rdH0sGclyxP+iXMp2GJq7Y PQSWON2tjlgcp3XC4iZvaDyKRw3dsqMXAZ7Fl1Q+rpRDgqYe3JtcaWMnGXDk5vxLBeb3 XwbGgCpb0bSoBMT5s7EzSbUzHF7tUnsnFc5hm9liOtcSD59qLtjQ71/86FLMd/5rYgUu Vg/A== X-Gm-Message-State: AOUpUlE/TtTU/3AXPHKnOuhvQSn1zvP48vtFt3jxRllBWYFgYSTPDC/Z UoIC8TdhLa8SrYrUhPCCe5WUcdet X-Google-Smtp-Source: AA+uWPwLFY6VQ+iPGcd6FKATe4yZjHd8GIqBzfeKUZugjVfpLfuz4WmaC2crum0fFCTXVayxDtHZIA== X-Received: by 2002:a19:eb83:: with SMTP id f3-v6mr21749734lfk.89.1535021041475; Thu, 23 Aug 2018 03:44:01 -0700 (PDT) Return-Path: Received: from jax.ideon.se ([85.235.10.227]) by smtp.gmail.com with ESMTPSA id y5-v6sm679771ljj.75.2018.08.23.03.44.00 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 23 Aug 2018 03:44:00 -0700 (PDT) From: Jens Wiklander To: u-boot@lists.denx.de Cc: Simon Glass , Igor Opaniuk , Tom Rini , Jaehoon Chung , Pierre Aubert , Albert Aribaud , Peter Griffin , Michal Simek , Jens Wiklander Subject: [PATCH v2 15/15] avb_verify: support using OP-TEE TA AVB Date: Thu, 23 Aug 2018 12:43:34 +0200 Message-Id: <20180823104334.16083-16-jens.wiklander@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180823104334.16083-1-jens.wiklander@linaro.org> References: <20180823104334.16083-1-jens.wiklander@linaro.org> With CONFIG_OPTEE_TA_AVB use the trusted application AVB provided by OP-TEE to manage rollback indexes and device lock status. Signed-off-by: Jens Wiklander --- common/avb_verify.c | 132 ++++++++++++++++++++++++++++++++++++++++++- doc/README.avb2 | 13 +++++ include/avb_verify.h | 4 ++ 3 files changed, 148 insertions(+), 1 deletion(-) -- 2.17.1 diff --git a/common/avb_verify.c b/common/avb_verify.c index 20e35ade3029..a50c05bf7847 100644 --- a/common/avb_verify.c +++ b/common/avb_verify.c @@ -10,6 +10,8 @@ #include #include #include +#include +#include const unsigned char avb_root_pub[1032] = { 0x0, 0x0, 0x10, 0x0, 0x55, 0xd9, 0x4, 0xad, 0xd8, 0x4, @@ -594,6 +596,79 @@ static AvbIOResult validate_vbmeta_public_key(AvbOps *ops, return AVB_IO_RESULT_OK; } +#ifdef CONFIG_OPTEE_TA_AVB +static void uuid_to_octets(u8 d[TEE_UUID_LEN], + const struct tee_optee_ta_uuid *s) +{ + d[0] = s->time_low >> 24; + d[1] = s->time_low >> 16; + d[2] = s->time_low >> 8; + d[3] = s->time_low; + d[4] = s->time_mid >> 8; + d[5] = s->time_mid; + d[6] = s->time_hi_and_version >> 8; + d[7] = s->time_hi_and_version; + memcpy(d + 8, s->clock_seq_and_node, sizeof(s->clock_seq_and_node)); +} + +static int get_open_session(struct AvbOpsData *ops_data) +{ + struct udevice *tee = NULL; + + while (!ops_data->tee) { + const struct tee_optee_ta_uuid uuid = TA_AVB_UUID; + struct tee_open_session_arg arg; + int rc; + + tee = tee_find_device(tee, NULL, NULL, NULL); + if (!tee) + return -ENODEV; + + memset(&arg, 0, sizeof(arg)); + uuid_to_octets(arg.uuid, &uuid); + rc = tee_open_session(tee, &arg, 0, NULL); + if (!rc) { + ops_data->tee = tee; + ops_data->session = arg.session; + } + } + + return 0; +} + +static AvbIOResult invoke_func(struct AvbOpsData *ops_data, u32 func, + ulong num_param, struct tee_param *param) +{ + struct tee_invoke_arg arg; + + if (get_open_session(ops_data)) + return AVB_IO_RESULT_ERROR_IO; + + memset(&arg, 0, sizeof(arg)); + arg.func = func; + arg.session = ops_data->session; + + if (tee_invoke_func(ops_data->tee, &arg, num_param, param)) + return AVB_IO_RESULT_ERROR_IO; + switch (arg.ret) { + case TEE_SUCCESS: + return AVB_IO_RESULT_OK; + case TEE_ERROR_OUT_OF_MEMORY: + return AVB_IO_RESULT_ERROR_OOM; + case TEE_ERROR_TARGET_DEAD: + /* + * The TA has paniced, close the session to reload the TA + * for the next request. + */ + tee_close_session(ops_data->tee, ops_data->session); + ops_data->tee = NULL; + return AVB_IO_RESULT_ERROR_IO; + default: + return AVB_IO_RESULT_ERROR_IO; + } +} +#endif + /** * read_rollback_index() - gets the rollback index corresponding to the * location of given by @out_rollback_index. @@ -609,6 +684,7 @@ static AvbIOResult read_rollback_index(AvbOps *ops, size_t rollback_index_slot, u64 *out_rollback_index) { +#ifndef CONFIG_OPTEE_TA_AVB /* For now we always return 0 as the stored rollback index. */ printf("%s not supported yet\n", __func__); @@ -616,6 +692,27 @@ static AvbIOResult read_rollback_index(AvbOps *ops, *out_rollback_index = 0; return AVB_IO_RESULT_OK; +#else + AvbIOResult rc; + struct tee_param param[2]; + + if (rollback_index_slot >= TA_AVB_MAX_ROLLBACK_LOCATIONS) + return AVB_IO_RESULT_ERROR_NO_SUCH_VALUE; + + memset(param, 0, sizeof(param)); + param[0].attr = TEE_PARAM_ATTR_TYPE_VALUE_INPUT; + param[0].u.value.a = rollback_index_slot; + param[1].attr = TEE_PARAM_ATTR_TYPE_VALUE_OUTPUT; + + rc = invoke_func(ops->user_data, TA_AVB_CMD_READ_ROLLBACK_INDEX, + ARRAY_SIZE(param), param); + if (rc) + return rc; + + *out_rollback_index = (u64)param[1].u.value.a << 32 | + (u32)param[1].u.value.b; + return AVB_IO_RESULT_OK; +#endif } /** @@ -633,10 +730,27 @@ static AvbIOResult write_rollback_index(AvbOps *ops, size_t rollback_index_slot, u64 rollback_index) { +#ifndef CONFIG_OPTEE_TA_AVB /* For now this is a no-op. */ printf("%s not supported yet\n", __func__); return AVB_IO_RESULT_OK; +#else + struct tee_param param[2]; + + if (rollback_index_slot >= TA_AVB_MAX_ROLLBACK_LOCATIONS) + return AVB_IO_RESULT_ERROR_NO_SUCH_VALUE; + + memset(param, 0, sizeof(param)); + param[0].attr = TEE_PARAM_ATTR_TYPE_VALUE_INPUT; + param[0].u.value.a = rollback_index_slot; + param[1].attr = TEE_PARAM_ATTR_TYPE_VALUE_INPUT; + param[1].u.value.a = (u32)(rollback_index >> 32); + param[1].u.value.b = (u32)rollback_index; + + return invoke_func(ops->user_data, TA_AVB_CMD_WRITE_ROLLBACK_INDEX, + ARRAY_SIZE(param), param); +#endif } /** @@ -652,6 +766,7 @@ static AvbIOResult write_rollback_index(AvbOps *ops, */ static AvbIOResult read_is_device_unlocked(AvbOps *ops, bool *out_is_unlocked) { +#ifndef CONFIG_OPTEE_TA_AVB /* For now we always return that the device is unlocked. */ printf("%s not supported yet\n", __func__); @@ -659,6 +774,16 @@ static AvbIOResult read_is_device_unlocked(AvbOps *ops, bool *out_is_unlocked) *out_is_unlocked = true; return AVB_IO_RESULT_OK; +#else + AvbIOResult rc; + struct tee_param param = { .attr = TEE_PARAM_ATTR_TYPE_VALUE_OUTPUT }; + + rc = invoke_func(ops->user_data, TA_AVB_CMD_READ_LOCK_STATE, 1, ¶m); + if (rc) + return rc; + *out_is_unlocked = !param.u.value.a; + return AVB_IO_RESULT_OK; +#endif } /** @@ -737,6 +862,11 @@ void avb_ops_free(AvbOps *ops) ops_data = ops->user_data; - if (ops_data) + if (ops_data) { +#ifdef CONFIG_OPTEE_TA_AVB + if (ops_data->tee) + tee_close_session(ops_data->tee, ops_data->session); +#endif avb_free(ops_data); + } } diff --git a/doc/README.avb2 b/doc/README.avb2 index 120279fedbe2..a29cee1b6f50 100644 --- a/doc/README.avb2 +++ b/doc/README.avb2 @@ -18,6 +18,13 @@ Integrity of the bootloader (U-boot BLOB and environment) is out of scope. For additional details check: https://android.googlesource.com/platform/external/avb/+/master/README.md +1.1. AVB using OP-TEE (optional) +--------------------------------- +If AVB is configured to use OP-TEE (see 4. below) rollback indexes and +device lock state are stored in RPMB. The RPMB partition is managed by +OP-TEE (https://www.op-tee.org/) which is a secure OS leveraging ARM +TrustZone. + 2. AVB 2.0 U-BOOT SHELL COMMANDS ----------------------------------- @@ -61,6 +68,12 @@ CONFIG_LIBAVB=y CONFIG_AVB_VERIFY=y CONFIG_CMD_AVB=y +In addtion optionally if storing rollback indexes in RPMB with help of +OP-TEE: +CONFIG_TEE=y +CONFIG_OPTEE=y +CONFIG_OPTEE_TA_AVB=y +CONFIG_SUPPORT_EMMC_RPMB=y Then add `avb verify` invocation to your android boot sequence of commands, e.g.: diff --git a/include/avb_verify.h b/include/avb_verify.h index eaa60f5393ef..a532a2331aea 100644 --- a/include/avb_verify.h +++ b/include/avb_verify.h @@ -27,6 +27,10 @@ struct AvbOpsData { struct AvbOps ops; int mmc_dev; enum avb_boot_state boot_state; +#ifdef CONFIG_OPTEE_TA_AVB + struct udevice *tee; + u32 session; +#endif }; struct mmc_part {