Received: from list by lists.gnu.org with archive (Exim 4.71) id 1VOeQ8-0002k2-Fw for mharc-grub-devel@gnu.org; Tue, 24 Sep 2013 22:01:40 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:35616) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VOeQ6-0002hz-4g for grub-devel@gnu.org; Tue, 24 Sep 2013 22:01:39 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1VOeQ4-0001VB-55 for grub-devel@gnu.org; Tue, 24 Sep 2013 22:01:38 -0400 Received: from mail-ye0-x249.google.com ([2607:f8b0:4002:c04::249]:52893) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VOeQ3-0001V5-Vx for grub-devel@gnu.org; Tue, 24 Sep 2013 22:01:36 -0400 Received: by mail-ye0-f201.google.com with SMTP id q3so624304yen.2 for ; Tue, 24 Sep 2013 19:01:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=dM5hGedZQ7Z5rk1NId8GnmJ7/4DuKhYSjd9Ua8epmZU=; b=CVKoRxrTQpoZLru/9eBQ50680FFNZL079yn99B/bbvzbxd1cXE8SkRQZxTZQTBzrle p4qCqyFzJo+2dlvmCp8L41omqcNnkAtX61/GWLNYi8Sm94DuazkUJSbE1Ko2O5fJ4vSV qs6oPLtjiBlf9ivGRg3eau1pJO9SbEeXf8btgtf/EZGKsR4maXeNuSq4vyw0hoS3vskn r6kSFrRcwVDjSjvn0yPfoRMBbjGr1LcU1eRUX1iAMBtPtpHfVE8ITCUs5AufvPo6GD+i bgQqzi1eGl3QwMWcIAeyKAUP72WZd49MmhxJ34rOYhHmvJ2Z7fUhPvlEJ/71CUS859Qp DynA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=dM5hGedZQ7Z5rk1NId8GnmJ7/4DuKhYSjd9Ua8epmZU=; b=eTdd68Put5ESZtDtiAtdnHfKkdhv69dFLY50rr1wg/KFP9RqccfR6gIqS5ntSdcA11 YulX9/EmHK0LuyQTnUaS/jp0RNgZNK/Dpwi208R9AY7qvuAlIUI71L28Am8Z/kJGBAN9 jZELCSCi2+vyJcjxUS4ErmON9kT67o/lJaf33/7unJOdkZyjRftUsyq5ms0EQCwcGBmm jCfRjNE7k/rSlaJdvWOzGVrsc8TrmFEWEmcYWgJCGSQmE3M8/X/JKtW70ufbjII6SygQ 3qKvb/sY7lnKMnQVApxfsx+obb7+qa0HQM/eMHuiMrGYJLNE3/VBr79rhF7QHMjLxS+g 8avQ== X-Gm-Message-State: ALoCoQnD5RVrSvmxH0yeE/LCZIdo3/QLXGC3RCZKCcrxk5agj2KWXWsNlY5abDyZCGgXaFf/24Yb055yt9FanbNGGqhgUx4rPE9wUj1MqKxtnJuFPuWAT8lInIKzhhPqjmQ38Z5tzHx9U4UXre2WdiR8YaOz/Uvr1AEe9oIBYNvxETWhaGqxchUWcND5CEKnIjQ1yjN948mCXG/ck41hkSeg0kptTi4txfYXZyA+nLRSkHoNJUgNpGg= X-Received: by 10.236.36.39 with SMTP id v27mr10481471yha.5.1380074495594; Tue, 24 Sep 2013 19:01:35 -0700 (PDT) Received: from corp2gmr1-1.hot.corp.google.com (corp2gmr1-1.hot.corp.google.com [172.24.189.92]) by gmr-mx.google.com with ESMTPS id a49si5007853yhc.5.1969.12.31.16.00.00 (version=TLSv1.1 cipher=AES128-SHA bits=128/128); Tue, 24 Sep 2013 19:01:35 -0700 (PDT) Received: from yinz.mtv.corp.google.com (yinz.mtv.corp.google.com [172.17.81.122]) by corp2gmr1-1.hot.corp.google.com (Postfix) with ESMTP id 5B12831C1F8; Tue, 24 Sep 2013 19:01:35 -0700 (PDT) Received: by yinz.mtv.corp.google.com (Postfix, from userid 184367) id 19859C3E8E; Tue, 24 Sep 2013 19:01:35 -0700 (PDT) From: Jon McCune To: grub-devel@gnu.org Subject: [PATCH v4 2/4] load_env support for whitelisting which variables are read from an env file, even if check_signatures=enforce Date: Tue, 24 Sep 2013 19:00:30 -0700 Message-Id: <1380074432-27299-3-git-send-email-jonmccune@google.com> X-Mailer: git-send-email 1.8.4 In-Reply-To: <1380074432-27299-1-git-send-email-jonmccune@google.com> References: <1380074432-27299-1-git-send-email-jonmccune@google.com> X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2607:f8b0:4002:c04::249 Cc: Jon McCune X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.14 Precedence: list Reply-To: The development of GNU GRUB List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 25 Sep 2013 02:01:39 -0000 This version of the patch implements whitelisting in the envblk subsystem, instead of in loadenv.c. It seems to be cleaner than the previous patches. This works by adding an open_envblk_file_untrusted() method that bypasses signature checking, but only if the invocation of load_env includes a whitelist of one or more environment variables that are to be read from the file. Only variables in this whitelist will actually be modified based on the contents of the file. This prevents a malicious environment block file from overwriting the value of security-critical environment variables such as check_signatures, while still allowing a properly constructed configuration file to offer "savedefault" and "one-shot" functionality. If no whitelist is provided, the behavior is the same as today: the entire input file must be signed when check_signatures=enforce. Signed-off-by: Jon McCune --- grub-core/commands/loadenv.c | 54 ++++++++++++++++++++++++++++++++++++++------ grub-core/lib/envblk.c | 20 ++++++++++++++-- include/grub/lib/envblk.h | 8 +++++++ util/grub-editenv.c | 2 +- 4 files changed, 74 insertions(+), 10 deletions(-) diff --git a/grub-core/commands/loadenv.c b/grub-core/commands/loadenv.c index a431499..16aafbf 100644 --- a/grub-core/commands/loadenv.c +++ b/grub-core/commands/loadenv.c @@ -76,6 +76,28 @@ open_envblk_file (char *filename) return grub_file_open (filename); } +/* Opens 'filename' with all filters disabled (in particular, the PUBKEY filter + that insists upon properly signed files, and any kind of compression, since + we don't want untrusted input being processed by complex compression + algorithms). */ +static grub_file_t +open_envblk_file_untrusted (char *filename) +{ + grub_file_filter_t curfilt[GRUB_FILE_FILTER_MAX]; + grub_file_t file; + + /* Temporarily disable all filters so as to read the untrusted file */ + grub_memcpy (curfilt, grub_file_filters_enabled, sizeof (curfilt)); + grub_file_filter_disable_all (); + /* TODO: Is there any possible code path due to open_envblk_file that could + * cause untrusted modules to be loaded? */ + file = open_envblk_file (filename); + /* Restore filters */ + grub_memcpy (grub_file_filters_enabled, curfilt, sizeof (curfilt)); + + return file; +} + static grub_envblk_t read_envblk_file (grub_file_t file) { @@ -122,16 +144,33 @@ set_var (const char *name, const char *value) return 0; } +/* Load environment variables from a file. Accepts a flag which can override + the file from which variables are read. If additional parameters are passed + (argc > 0), then those are interpreted as a whitelist of environment + variables whose values can be changed based on the contents of the file, and + the file is never signature-checked. + Otherwise, all variables from the file are processed, and the file must be + properly signed when check_signatures=enforce. */ static grub_err_t -grub_cmd_load_env (grub_extcmd_context_t ctxt, - int argc __attribute__ ((unused)), - char **args __attribute__ ((unused))) +grub_cmd_load_env (grub_extcmd_context_t ctxt, int argc, char **args) { struct grub_arg_list *state = ctxt->state; grub_file_t file; grub_envblk_t envblk; + grub_env_whitelist_t whitelist; + + whitelist.len = argc; + whitelist.list = args; + + /* argc > 0 indicates caller provided a whitelist of variables to read. + Open the envblk file even if check_signatures=enforce and the file is + not properly signed, since we will only update environment variables + whose names are present in the whitelist. */ + if (argc > 0) + file = open_envblk_file_untrusted ((state[0].set) ? state[0].arg : 0); + else + file = open_envblk_file ((state[0].set) ? state[0].arg : 0); - file = open_envblk_file ((state[0].set) ? state[0].arg : 0); if (!file) return grub_errno; @@ -139,7 +178,7 @@ grub_cmd_load_env (grub_extcmd_context_t ctxt, if (!envblk) goto fail; - grub_envblk_iterate (envblk, set_var); + grub_envblk_iterate (envblk, argc > 0 ? &whitelist : NULL, set_var); grub_envblk_close (envblk); fail: @@ -172,7 +211,7 @@ grub_cmd_list_env (grub_extcmd_context_t ctxt, if (!envblk) goto fail; - grub_envblk_iterate (envblk, print_var); + grub_envblk_iterate (envblk, NULL, print_var); grub_envblk_close (envblk); fail: @@ -387,7 +426,8 @@ static grub_extcmd_t cmd_load, cmd_list, cmd_save; GRUB_MOD_INIT (loadenv) { cmd_load = - grub_register_extcmd ("load_env", grub_cmd_load_env, 0, N_("[-f FILE]"), + grub_register_extcmd ("load_env", grub_cmd_load_env, 0, + N_("[-f FILE] [whitelisted_variable_name] [...]"), N_("Load variables from environment block file."), options); cmd_list = diff --git a/grub-core/lib/envblk.c b/grub-core/lib/envblk.c index 311927b..50d87c8 100644 --- a/grub-core/lib/envblk.c +++ b/grub-core/lib/envblk.c @@ -223,8 +223,22 @@ grub_envblk_delete (grub_envblk_t envblk, const char *name) } } +static int +test_whitelist_membership (const char* name, + const grub_env_whitelist_t* whitelist) +{ + grub_size_t i; + + for (i = 0; i < whitelist->len; i++) + if (grub_strcmp (name, whitelist->list[i]) == 0) + return 1; /* found it */ + + return 0; /* not found */ +} + void grub_envblk_iterate (grub_envblk_t envblk, + const grub_env_whitelist_t* whitelist, int hook (const char *name, const char *value)) { char *p, *pend; @@ -240,7 +254,7 @@ grub_envblk_iterate (grub_envblk_t envblk, char *value; char *name_start, *name_end, *value_start; char *q; - int ret; + int ret = 0; name_start = p; while (p < pend && *p != '=') @@ -285,7 +299,9 @@ grub_envblk_iterate (grub_envblk_t envblk, } *q = '\0'; - ret = hook (name, value); + if (!whitelist || + (whitelist && test_whitelist_membership (name, whitelist))) + ret = hook (name, value); grub_free (name); if (ret) return; diff --git a/include/grub/lib/envblk.h b/include/grub/lib/envblk.h index 368ba53..8857058 100644 --- a/include/grub/lib/envblk.h +++ b/include/grub/lib/envblk.h @@ -31,10 +31,18 @@ struct grub_envblk }; typedef struct grub_envblk *grub_envblk_t; +struct grub_env_whitelist +{ + grub_size_t len; + char **list; +}; +typedef struct grub_env_whitelist grub_env_whitelist_t; + grub_envblk_t grub_envblk_open (char *buf, grub_size_t size); int grub_envblk_set (grub_envblk_t envblk, const char *name, const char *value); void grub_envblk_delete (grub_envblk_t envblk, const char *name); void grub_envblk_iterate (grub_envblk_t envblk, + const grub_env_whitelist_t *whitelist, int hook (const char *name, const char *value)); void grub_envblk_close (grub_envblk_t envblk); diff --git a/util/grub-editenv.c b/util/grub-editenv.c index 9b51acf..f9b6959 100644 --- a/util/grub-editenv.c +++ b/util/grub-editenv.c @@ -197,7 +197,7 @@ list_variables (const char *name) grub_envblk_t envblk; envblk = open_envblk_file (name); - grub_envblk_iterate (envblk, print_var); + grub_envblk_iterate (envblk, NULL, print_var); grub_envblk_close (envblk); } -- 1.8.4