Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1kODOS-0003Sd-V7 for mharc-grub-devel@gnu.org; Fri, 02 Oct 2020 01:14:08 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:32888) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kODOB-0003Jd-BT for grub-devel@gnu.org; Fri, 02 Oct 2020 01:13:53 -0400 Received: from mail-pf1-x444.google.com ([2607:f8b0:4864:20::444]:37646) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1kODO9-00055k-6I for grub-devel@gnu.org; Fri, 02 Oct 2020 01:13:51 -0400 Received: by mail-pf1-x444.google.com with SMTP id 144so460730pfb.4 for ; Thu, 01 Oct 2020 22:13:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=axtens.net; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=gqH1vM0Vt4hFwf7qDyOIcfcTnMJT+A0t+EtU+YuLlDI=; b=GfImYXjynQ+GhzKPEla9YQVte2pnSbQvA2SprGpTxQJsGYDrKoFYb/0vkTre0XvqqM hhLyVOmfMQWC6i5iZl+gAKVwM99rR6zcyvadwLNSDApsy0cI/gyP7XdIYMVTgKM6frob CuTUHvzuGqmeJPPn3dkAX4FND7DkqQPusaxHY= 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=gqH1vM0Vt4hFwf7qDyOIcfcTnMJT+A0t+EtU+YuLlDI=; b=J19f1h57dZrj332gqEiXthJ+pfMPHf2aP/reYWwN4iw1NfO+fvFHs/QCOP99swZ2tZ fJjxgLhxFzX/cfnvrDkey0mMfV28m6UqJFKHtjrLmHxTAcVlRPZcfx6wnEVMX2vNdfWa EIamIRTfpAN20jwGO02Cdif94X2783/q+xudBWyEfYZEt5qSTEANs5AB8t3Gm2YeiyDU DdX88WaLpJ8u/AfLkFQ23Y6c8wLk+AqJffZuUQwJ/rgrLrvzq17zxqmQL4suEws8X3A4 EO7goyOUcRFgL2UoVS4jR8ZLqtU3+zrhbQP0/IjxvRALQMuYYyvB1c/u4xl2vJAF8lfP pvww== X-Gm-Message-State: AOAM5327SLmFZbdsOV3E7cl7Y91fUBV3JauIxIsAWfNt+VrTVeVXKkBk 860RPkqi0xrHjLeAuprmRDUgH2vanVHwaQ== X-Google-Smtp-Source: ABdhPJwPDBILXVofuJOpFBtYKEHVomHFcHBz4iosS1wW+mLgAZ1NwibDELJPOlL+UiumQbm8GMaEqA== X-Received: by 2002:a05:6a00:15c8:b029:142:2501:35ca with SMTP id o8-20020a056a0015c8b0290142250135camr877501pfu.42.1601615626067; Thu, 01 Oct 2020 22:13:46 -0700 (PDT) Received: from localhost (2001-44b8-111e-5c00-804e-30c2-4157-6268.static.ipv6.internode.on.net. [2001:44b8:111e:5c00:804e:30c2:4157:6268]) by smtp.gmail.com with ESMTPSA id l12sm333988pgs.1.2020.10.01.22.13.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 01 Oct 2020 22:13:45 -0700 (PDT) From: Daniel Axtens To: grub-devel@gnu.org Cc: rashmica.g@gmail.com, alastair@d-silva.org, Daniel Axtens Subject: [PATCH 04/18] verifiers: factor unsafe module handling out of shim_lock Date: Fri, 2 Oct 2020 15:13:07 +1000 Message-Id: <20201002051321.1538488-5-dja@axtens.net> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20201002051321.1538488-1-dja@axtens.net> References: <20201002051321.1538488-1-dja@axtens.net> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Received-SPF: pass client-ip=2607:f8b0:4864:20::444; envelope-from=dja@axtens.net; helo=mail-pf1-x444.google.com X-detected-operating-system: by eggs.gnu.org: No matching host in p0f cache. That's all we know. X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 02 Oct 2020 05:13:53 -0000 Other verifiers that implement secure boot may want to be able to reuse the same list and behaviour. Signed-off-by: Daniel Axtens --- grub-core/commands/efi/shim_lock.c | 45 +++++++++++------------------ grub-core/commands/verifiers.c | 46 ++++++++++++++++++++++++++++++ include/grub/verify.h | 13 +++++++++ 3 files changed, 75 insertions(+), 29 deletions(-) diff --git a/grub-core/commands/efi/shim_lock.c b/grub-core/commands/efi/shim_lock.c index 764098cfc83e..bad594b4620d 100644 --- a/grub-core/commands/efi/shim_lock.c +++ b/grub-core/commands/efi/shim_lock.c @@ -42,16 +42,12 @@ typedef struct grub_efi_shim_lock_protocol grub_efi_shim_lock_protocol_t; static grub_efi_guid_t shim_lock_guid = GRUB_EFI_SHIM_LOCK_GUID; static grub_efi_shim_lock_protocol_t *sl; -/* List of modules which cannot be loaded if UEFI secure boot mode is enabled. */ -static const char * const disabled_mods[] = {"iorw", "memrw", "wrmsr", NULL}; - static grub_err_t shim_lock_init (grub_file_t io, enum grub_file_type type, void **context __attribute__ ((unused)), enum grub_verify_flags *flags) { - const char *b, *e; - int i; + const char *dangerous_mod; *flags = GRUB_VERIFY_FLAGS_SKIP_VERIFICATION; @@ -61,22 +57,13 @@ shim_lock_init (grub_file_t io, enum grub_file_type type, switch (type & GRUB_FILE_TYPE_MASK) { case GRUB_FILE_TYPE_GRUB_MODULE: - /* Establish GRUB module name. */ - b = grub_strrchr (io->name, '/'); - e = grub_strrchr (io->name, '.'); - - b = b ? (b + 1) : io->name; - e = e ? e : io->name + grub_strlen (io->name); - e = (e > b) ? e : io->name + grub_strlen (io->name); - - for (i = 0; disabled_mods[i]; i++) - if (!grub_strncmp (b, disabled_mods[i], grub_strlen (b) - grub_strlen (e))) - { - grub_error (GRUB_ERR_ACCESS_DENIED, - N_("module cannot be loaded in UEFI secure boot mode: %s"), - io->name); - return GRUB_ERR_ACCESS_DENIED; - } + if (grub_is_dangerous_module (io)) + { + grub_error (GRUB_ERR_ACCESS_DENIED, + N_("module cannot be loaded in UEFI secure boot mode: %s"), + io->name); + return GRUB_ERR_ACCESS_DENIED; + } /* Fall through. */ @@ -91,14 +78,14 @@ shim_lock_init (grub_file_t io, enum grub_file_type type, case GRUB_FILE_TYPE_BSD_KERNEL: case GRUB_FILE_TYPE_XNU_KERNEL: case GRUB_FILE_TYPE_PLAN9_KERNEL: - for (i = 0; disabled_mods[i]; i++) - if (grub_dl_get (disabled_mods[i])) - { - grub_error (GRUB_ERR_ACCESS_DENIED, - N_("cannot boot due to dangerous module in memory: %s"), - disabled_mods[i]); - return GRUB_ERR_ACCESS_DENIED; - } + dangerous_mod = grub_dangerous_module_loaded (); + if (dangerous_mod) + { + grub_error (GRUB_ERR_ACCESS_DENIED, + N_("cannot boot due to dangerous module in memory: %s"), + dangerous_mod); + return GRUB_ERR_ACCESS_DENIED; + } *flags = GRUB_VERIFY_FLAGS_SINGLE_CHUNK; diff --git a/grub-core/commands/verifiers.c b/grub-core/commands/verifiers.c index aae4d84bb118..1d67a2541dae 100644 --- a/grub-core/commands/verifiers.c +++ b/grub-core/commands/verifiers.c @@ -221,6 +221,52 @@ grub_verify_string (char *str, enum grub_verify_string_type type) return GRUB_ERR_NONE; } +/* List of modules which may allow for verifcation to be bypassed. */ +static const char *const disabled_mods[] = { "iorw", "memrw", "wrmsr", NULL }; + +/* + * Does the module in file `io' allow for the a verifier to be bypassed? + * + * Returns 1 if so, otherwise 0. + */ +char +grub_is_dangerous_module (grub_file_t io) +{ + char *b, *e; + int i; + + /* Establish GRUB module name. */ + b = grub_strrchr (io->name, '/'); + e = grub_strrchr (io->name, '.'); + + b = b ? (b + 1) : io->name; + e = e ? e : io->name + grub_strlen (io->name); + e = (e > b) ? e : io->name + grub_strlen (io->name); + + for (i = 0; disabled_mods[i]; i++) + if (!grub_strncmp (b, disabled_mods[i], + grub_strlen (b) - grub_strlen (e))) + return 1; + return 0; +} + +/* + * Is there already an unsafe module in memory? + * Returns the name if one is loaded, otherwise NULL. + */ +const char * +grub_dangerous_module_loaded (void) +{ + int i; + + for (i = 0; disabled_mods[i]; i++) + if (grub_dl_get (disabled_mods[i])) + { + return disabled_mods[i]; + } + return NULL; +} + GRUB_MOD_INIT(verifiers) { grub_file_filter_register (GRUB_FILE_FILTER_VERIFY, grub_verifiers_open); diff --git a/include/grub/verify.h b/include/grub/verify.h index ea049143368e..8c2de132051d 100644 --- a/include/grub/verify.h +++ b/include/grub/verify.h @@ -81,4 +81,17 @@ grub_verifier_unregister (struct grub_file_verifier *ver) grub_err_t grub_verify_string (char *str, enum grub_verify_string_type type); +/* + * Does the module in file `io' allow for the a verifier to be bypassed? + * + * Returns 1 if so, otherwise 0. + */ +char grub_is_dangerous_module (grub_file_t io); + +/* + * Is there already an unsafe module in memory? + * Returns the name if one is loaded, otherwise NULL. + */ +const char *grub_dangerous_module_loaded (void); + #endif /* ! GRUB_VERIFY_HEADER */ -- 2.25.1