From patchwork Fri Dec 22 21:13:36 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Quentin Schulz X-Patchwork-Id: 122661 Delivered-To: patch@linaro.org Received: by 10.140.22.227 with SMTP id 90csp2743575qgn; Fri, 22 Dec 2017 13:15:33 -0800 (PST) X-Google-Smtp-Source: ACJfBovKVxH8sSG5k7vfUCs7OEFc9UvrrHcqe1clupAJNoK6qAWxh9zro9Pt8Ai8nLDeLrHGUpta X-Received: by 10.80.146.207 with SMTP id l15mr16734461eda.194.1513977333561; Fri, 22 Dec 2017 13:15:33 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1513977333; cv=none; d=google.com; s=arc-20160816; b=QvaVEiLvy8Bbugjl39ICuzyfMooh+2VvvNtfaEVOytjEjZJZbY6kYTab2w8SQ5bUdl C0QhZNx6zau1CSxUTcbsQjOqyFBGIuGHV4rDSUpYwIqtWhnL2Auzph5+BVgTP2C+654B QdBNd6tFYJ4bB3wUsNacFt0ihdPMFywWOsRV4630Ip50cmXtl76U32xrLBYuvgeQooBf 3UEs6+AbkASkY8Jpgwy1aaSKirlHSs//MO40IH1n8BcnqEv+5NyZ0GA67q/LI0ed53se VYwuUxNv8INYlvhw4WVyy7qoRap0W0CCZupT+JuOg91gik2hlyftK/ayk2FpjzmR/lJ7 4RtA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:mime-version :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:cc:references:in-reply-to:references :in-reply-to:message-id:date:to:from:arc-authentication-results; bh=G//d1sIbe9mtH7J5iCyvrvWGzVyCaq+bXIcJJsv8D4s=; b=eoxOVGCMPtXOBZTUm+OXW3FhkRRf5hPly2ee/r3Na5KDnl2SAVKDiDeg0hNMSxkBnJ A5aXMfd57MGcC4YNdbmV3O6HyxZ6LpGPuY6vUqmLj2fVSCtO4YFpAfLw+PWsALrsYJ3u 4dOXhXxsWnrPTn/UEc1pMkpDCqZ2c+fpxvoDRkzNvfL+FPVa8Os6z3X9MbVvcKAqo4Yo Wm3FiBjYQjHmNXLneVHEREHccVfPbIQx/qSYQWeqnPUt6VEH7QWUPibyQq9gjGzGuJZe wwT34Sf++xdQXcrZoKMc9cBZGxkdgEaS/aKtQ5N9SMQSVpoukmI8OityE+Iu3Vx7XrjL aTpg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of u-boot-bounces@lists.denx.de designates 81.169.180.215 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de Return-Path: Received: from lists.denx.de (dione.denx.de. [81.169.180.215]) by mx.google.com with ESMTP id l47si3926004edc.253.2017.12.22.13.15.33; Fri, 22 Dec 2017 13:15:33 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of u-boot-bounces@lists.denx.de designates 81.169.180.215 as permitted sender) client-ip=81.169.180.215; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of u-boot-bounces@lists.denx.de designates 81.169.180.215 as permitted sender) smtp.mailfrom=u-boot-bounces@lists.denx.de Received: by lists.denx.de (Postfix, from userid 105) id 2149AC2221D; Fri, 22 Dec 2017 21:14:55 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on lists.denx.de X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=unavailable autolearn_force=no version=3.4.0 Received: from lists.denx.de (localhost [IPv6:::1]) by lists.denx.de (Postfix) with ESMTP id 7FA0EC21FE3; Fri, 22 Dec 2017 21:14:17 +0000 (UTC) Received: by lists.denx.de (Postfix, from userid 105) id 4E97FC21F2D; Fri, 22 Dec 2017 21:14:14 +0000 (UTC) Received: from mail.free-electrons.com (mail.free-electrons.com [62.4.15.54]) by lists.denx.de (Postfix) with ESMTP id 0A3B1C21EF1 for ; Fri, 22 Dec 2017 21:14:14 +0000 (UTC) Received: by mail.free-electrons.com (Postfix, from userid 110) id 2B5E2207C8; Fri, 22 Dec 2017 22:14:13 +0100 (CET) Received: from localhost.localdomain (unknown [80.12.41.248]) by mail.free-electrons.com (Postfix) with ESMTPSA id 8B319203A1; Fri, 22 Dec 2017 22:14:12 +0100 (CET) From: Quentin Schulz To: jagan@openedev.com, maxime.ripard@free-electrons.com, hdegoede@redhat.com, sjg@chromium.org, wd@denx.de, andre.przywara@arm.com, lukma@denx.de Date: Fri, 22 Dec 2017 22:13:36 +0100 Message-Id: X-Mailer: git-send-email 2.14.1 In-Reply-To: References: In-Reply-To: References: Cc: thomas.petazzoni@free-electrons.com, u-boot@lists.denx.de Subject: [U-Boot] [PATCH 02/11] lib: hashtable: support whitelisting env variables X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.18 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" When passing the vars array to himport_r, variables in the array that are not in the loaded environment are removed from the current environment. Of course, this isn't suitable for whitelisting some variables. Let's introduce a whitelisting boolean that will not remove array variables that are not in the loaded environment from the current environment. The remaining of the code will rightfully override any variable that already exists. Signed-off-by: Quentin Schulz --- board/sunxi/board.c | 2 +- cmd/nvedit.c | 2 +- env/common.c | 6 +++--- include/search.h | 2 +- lib/hashtable.c | 17 ++++++++++++++++- 5 files changed, 22 insertions(+), 7 deletions(-) diff --git a/board/sunxi/board.c b/board/sunxi/board.c index 8891961..aac4904 100644 --- a/board/sunxi/board.c +++ b/board/sunxi/board.c @@ -647,7 +647,7 @@ static void parse_spl_header(const uint32_t spl_addr) * import -t" the string(s) at fel_script_address right away. */ himport_r(&env_htab, (char *)(uintptr_t)spl->fel_script_address, - spl->fel_uEnv_length, '\n', H_NOCLEAR, 0, 0, NULL); + spl->fel_uEnv_length, '\n', H_NOCLEAR, 0, 0, NULL, 0); return; } /* otherwise assume .scr format (mkimage-type script) */ diff --git a/cmd/nvedit.c b/cmd/nvedit.c index a690d74..c00e1da 100644 --- a/cmd/nvedit.c +++ b/cmd/nvedit.c @@ -1079,7 +1079,7 @@ static int do_env_import(cmd_tbl_t *cmdtp, int flag, } if (himport_r(&env_htab, ptr, size, sep, del ? 0 : H_NOCLEAR, - crlf_is_lf, 0, NULL) == 0) { + crlf_is_lf, 0, NULL, false) == 0) { pr_err("Environment import failed: errno = %d\n", errno); return 1; } diff --git a/env/common.c b/env/common.c index fb66432..9d97541 100644 --- a/env/common.c +++ b/env/common.c @@ -83,7 +83,7 @@ void set_default_env(const char *s) if (himport_r(&env_htab, (char *)default_environment, sizeof(default_environment), '\0', flags, 0, - 0, NULL) == 0) + 0, NULL, false) == 0) pr_err("Environment import failed: errno = %d\n", errno); gd->flags |= GD_FLG_ENV_READY; @@ -100,7 +100,7 @@ int set_default_vars(int nvars, char * const vars[]) */ return himport_r(&env_htab, (const char *)default_environment, sizeof(default_environment), '\0', - H_NOCLEAR | H_INTERACTIVE, 0, nvars, vars); + H_NOCLEAR | H_INTERACTIVE, 0, nvars, vars, 0); } #ifdef CONFIG_ENV_AES @@ -178,7 +178,7 @@ int env_import(const char *buf, int check) } if (himport_r(&env_htab, (char *)ep->data, ENV_SIZE, '\0', 0, 0, - 0, NULL)) { + 0, NULL, false)) { gd->flags |= GD_FLG_ENV_READY; return 1; } diff --git a/include/search.h b/include/search.h index df5d61c..ed2d9bf 100644 --- a/include/search.h +++ b/include/search.h @@ -101,7 +101,7 @@ extern ssize_t hexport_r(struct hsearch_data *__htab, extern int himport_r(struct hsearch_data *__htab, const char *__env, size_t __size, const char __sep, int __flag, int __crlf_is_lf, int nvars, - char * const vars[]); + char * const vars[], bool whitelisting); /* Walk the whole table calling the callback on each element */ extern int hwalk_r(struct hsearch_data *__htab, int (*callback)(ENTRY *)); diff --git a/lib/hashtable.c b/lib/hashtable.c index f088477..432fe14 100644 --- a/lib/hashtable.c +++ b/lib/hashtable.c @@ -772,11 +772,17 @@ static int drop_var_from_set(const char *name, int nvars, char * vars[]) * * In theory, arbitrary separator characters can be used, but only * '\0' and '\n' have really been tested. + * + * "whitelisting" makes the function to not remove variables from the + * env that were not found in "vars". + * if "whitelisting" is false, the function remove variables from env + * that were not found in "vars". */ int himport_r(struct hsearch_data *htab, const char *env, size_t size, const char sep, int flag, - int crlf_is_lf, int nvars, char * const vars[]) + int crlf_is_lf, int nvars, char * const vars[], + bool whitelisting) { char *data, *sp, *dp, *name, *value; char *localvars[nvars]; @@ -935,6 +941,14 @@ int himport_r(struct hsearch_data *htab, free(data); /* process variables which were not considered */ + /* + * If we are importing variables from a second env and checking they're + * whitelisted, we don't want to delete the variables in current env + * because it was not in the whitelist. + */ + if (whitelisting) + goto out; + for (i = 0; i < nvars; i++) { if (localvars[i] == NULL) continue; @@ -952,6 +966,7 @@ int himport_r(struct hsearch_data *htab, printf("WARNING: '%s' not in imported env, deleting it!\n", localvars[i]); } +out: debug("INSERT: done\n"); return 1; /* everything OK */ }