From patchwork Tue Sep 25 14:40:20 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jens Wiklander X-Patchwork-Id: 147481 Delivered-To: patches@linaro.org Received: by 2002:a2e:8595:0:0:0:0:0 with SMTP id b21-v6csp812785lji; Tue, 25 Sep 2018 07:40:54 -0700 (PDT) X-Received: by 2002:a2e:5015:: with SMTP id e21-v6mr1196438ljb.84.1537886453982; Tue, 25 Sep 2018 07:40:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1537886453; cv=none; d=google.com; s=arc-20160816; b=QcFx+wz+euVeQGLMy6HGPj8w4HHXKtxaM54lZxYZo7gBVjvYFZhUuwPfG5/uwnTPy2 KnirMxpm9ItSug1YZ79Q0hweAmnxw8YwSRnWU1R4SKlRMEkc+PnCRYfJWAR2RPWhvui4 6peMoPIkjrENL4sEQjpXaS7e/c49pa/N+Jt1QZjQGmJzQaLLwIQqtl5gPsAND01eT/0r bcG1u5qRJQcVaH2E+cftyDc4MSbJL6mvSyoNhDMgSIBpivk+Yyb67qK3SaXCXlyoMhig y2UlIpGrvrg+v+5Od5IeJsyYbmhHHHXQ0TuYxSqU30kNHQ9yzaQAjlPYJxmH3EsOP3XJ 6jzA== 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; bh=BFFaBVShsZ3EJnL5xajBY7P/235gApvSKtMm9gRfjLA=; b=qxbaHxTl2J3EdzCyOByXThmZf02r1nlou4UZxuWBxNwYtlKgqjcB60mYC2MP0jv016 VRTC0rmc0UO+0Hd58NuBFrib8juTmtt7L7Nb94m3cG9wRPN/WUPH+Llw43NwLJImL/UK uXVHvAkYcoAknvasTthCQNpnBo+h8Nb+IXvgrBQx3vua3LTRcWtunADFm7WB+rncEYtZ ChU1PKqBOQowRK88E0hrqWc66LrkjIwAI2jAbkOwOgrvGwjaCfGNZBrQZvezuK2hT2qm ZeUppWZ+34ldVADJOcJAFMmz9O2t54YBzu+s93UIom20rOs6lP7hbK04Jasf7l6hl/em 4tbA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Lhxlb3AV; spf=pass (google.com: domain of jens.wiklander@linaro.org designates 209.85.220.65 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-f65.google.com (mail-sor-f65.google.com. [209.85.220.65]) by mx.google.com with SMTPS id a26-v6sor1620078ljj.42.2018.09.25.07.40.53 for (Google Transport Security); Tue, 25 Sep 2018 07:40:53 -0700 (PDT) Received-SPF: pass (google.com: domain of jens.wiklander@linaro.org designates 209.85.220.65 as permitted sender) client-ip=209.85.220.65; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=Lhxlb3AV; spf=pass (google.com: domain of jens.wiklander@linaro.org designates 209.85.220.65 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=BFFaBVShsZ3EJnL5xajBY7P/235gApvSKtMm9gRfjLA=; b=Lhxlb3AVQmfZwyVyk0Cpirv9yy1aLXRRhXR8j/MrjZck4Lss8OAIBTV9Pw7lA3DjWW 5Vwcy3V90DGm31D1GYF9cNARwj1tQUNSVCzf0RnnAuKEMqdyNPE2tyLk8gobiF3TTrud m6Dr+lJ/RuP88zI3KjH4MXf4K5ICpd4RrMz/Y= 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=BFFaBVShsZ3EJnL5xajBY7P/235gApvSKtMm9gRfjLA=; b=W+ha7V3Dc8NwEGo2UNGpTkmQX4R5z2vGkwdJscgAjCTh+4W3yh+LppLUHRlEMWf3I2 PWuzAlJLib9rsOwwtbqm4kJRvTm1XvJviMOnKufaa0uhsLth3kZBHVlb5ny7q4sXZjqo i+KSOO9uKBDOECJ/33FMhjRr+R8L+O0ldS9+Uk6WMj/wFz9i4Zo2wubp7x0d0sCaot/+ q520K/d/sLl8CyaiAa1wIUBx8tCFUxydOGSHgnz/FpW+rRhmX47hSgMMpMk8+T0WTmSL UT0v+iX4EoIU1h2tHDTHNhmcj/3TlbnnsGCpyOGJPsX+h67e1KOS9dY0zaSr+q5vOIs+ /PAA== X-Gm-Message-State: ABuFfohRBESyJIAZ8zMaQ1scPBtIuG/A3Vhs6zqH8c/u41ho4IVMFNLc j96oSWRzfQUrhRyFtbsw28owe83K X-Google-Smtp-Source: ACcGV62Im5HNZhyPE+6bnSg7c3vnN53ToCL6GZnsrqh3YaPBJj8tdE5CNKn4kajHUoCunnXDI83kMA== X-Received: by 2002:a2e:360c:: with SMTP id d12-v6mr1177808lja.92.1537886453745; Tue, 25 Sep 2018 07:40:53 -0700 (PDT) Return-Path: Received: from jax.urgonet (h-84-105.A175.priv.bahnhof.se. [79.136.84.105]) by smtp.gmail.com with ESMTPSA id g14-v6sm14483lja.96.2018.09.25.07.40.52 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 25 Sep 2018 07:40:52 -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 v4 16/19] avb_verify: support using OP-TEE TA AVB Date: Tue, 25 Sep 2018 16:40:20 +0200 Message-Id: <20180925144023.24555-17-jens.wiklander@linaro.org> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180925144023.24555-1-jens.wiklander@linaro.org> References: <20180925144023.24555-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. Reviewed-by: Simon Glass Signed-off-by: Jens Wiklander --- common/avb_verify.c | 118 ++++++++++++++++++++++++++++++++++++++++++- doc/README.avb2 | 13 +++++ include/avb_verify.h | 4 ++ 3 files changed, 134 insertions(+), 1 deletion(-) -- 2.17.1 diff --git a/common/avb_verify.c b/common/avb_verify.c index f8c6ae5566ae..a8c5a3e7db36 100644 --- a/common/avb_verify.c +++ b/common/avb_verify.c @@ -10,6 +10,8 @@ #include #include #include +#include +#include static const unsigned char avb_root_pub[1032] = { 0x0, 0x0, 0x10, 0x0, 0x55, 0xd9, 0x4, 0xad, 0xd8, 0x4, @@ -600,6 +602,65 @@ static AvbIOResult validate_vbmeta_public_key(AvbOps *ops, return AVB_IO_RESULT_OK; } +#ifdef CONFIG_OPTEE_TA_AVB +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)); + tee_optee_ta_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. @@ -615,6 +676,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__); @@ -622,6 +684,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 } /** @@ -639,10 +722,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 } /** @@ -658,6 +758,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__); @@ -665,6 +766,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 } /** @@ -774,6 +885,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 {