Received: by 2002:ac0:8c9a:0:0:0:0:0 with SMTP id r26csp3870499ima; Mon, 4 Feb 2019 06:35:33 -0800 (PST) X-Google-Smtp-Source: AHgI3IYhG/04ftJwgZ0/XlcjOhRdaRGz26vvjh5iJpGfM6jbd6V2vXvstp06dYGtYKhpqyo2herN X-Received: by 2002:a65:53c9:: with SMTP id z9mr4088591pgr.403.1549290933395; Mon, 04 Feb 2019 06:35:33 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1549290933; cv=none; d=google.com; s=arc-20160816; b=Fi0xOwUfJjuq+JHCU/Be4mlZrmFfl8jk5hHG/X47k6Ub5I8xa+tCQP2xYWzf+a8M2O +D3LSaChU42FYNUX8HodGHgo2VW2LBVmCcNPOH2Am6/mTAtsnCXWAToInHp6A4hymmOQ wDKyXq6TKbajs9WaO5BFKF/0KaI6r5+UXvZ396R6w8BwaENgX51+cqjbclZcmQBGWhfZ XiV8YuPNa/xbePSmMVAbl3CbZkBUl/oNZk7YhlySwZyjeaZ8WWYCZzEHhc0SxfHRS9mt xwvcThQwin9COLe2lUrklkqxpp6Hqved3p4PFOh+DMk3PYjHabT0dWNCW7fiSn3m4+eL xDWA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from; bh=iiEcOrQfU6UEmVF+H6zyXZ4D6hQB9JcryadlJWMZafc=; b=I1b99x5dk4jQiig/+2LNcpet0BfdCvsIEH2I8sL7Gw4Ls0pvlmkFg+YazCftW1vooj lUQJ2GlNnFo03fFeYNQrbAMASkvEkzBjkykeRFGQBcq35yMI9YLGSbgbWeUsbYv50YtM egmNTrxi15AWlgJz4kSOPPWslgHdQyKaBej0P7A7AknDMO3A3/emam+l5niYipzOnBCQ 18VI/N6ywcEbH490qYuzGfyp//umnEtITC6XuxPWYMRwfcUgHuCUyTeCKBNPNSu7AvpH WDC9SMp4590f31oNHOIVIqAA3NWozVmDurWmaM+KDXxf5Kbu4cLuZgeQBPWT/Aehylwf BdZA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id o28si179913pgm.238.2019.02.04.06.35.16; Mon, 04 Feb 2019 06:35:33 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731481AbfBDN5L (ORCPT + 99 others); Mon, 4 Feb 2019 08:57:11 -0500 Received: from mx2.suse.de ([195.135.220.15]:35790 "EHLO mx1.suse.de" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1731441AbfBDN5I (ORCPT ); Mon, 4 Feb 2019 08:57:08 -0500 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id 586A8ADD4; Mon, 4 Feb 2019 13:57:07 +0000 (UTC) From: Petr Mladek To: Jiri Kosina , Josh Poimboeuf , Miroslav Benes Cc: Jason Baron , Joe Lawrence , Evgenii Shatokhin , live-patching@vger.kernel.org, linux-kernel@vger.kernel.org, Petr Mladek Subject: [PATCH v2 3/4] livepatch: Proper error handling in the shadow variables selftest Date: Mon, 4 Feb 2019 14:56:52 +0100 Message-Id: <20190204135653.31406-4-pmladek@suse.com> X-Mailer: git-send-email 2.13.7 In-Reply-To: <20190204135653.31406-1-pmladek@suse.com> References: <20190204135653.31406-1-pmladek@suse.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Add proper error handling when allocating or getting shadow variables in the selftest. It prevents an invalid pointer access in some situations. It shows the good programming practice in the others. The error codes are just the best guess and specific for this particular test. In general, klp_shadow_alloc() returns NULL also when the given shadow variable has already been allocated. In addition, both klp_shadow_alloc() and klp_shadow_get_or_alloc() might fail from other reasons when the constructor fails. Note, that the error code is not really important even in the real life. The use of shadow variables should be transparent for the original livepatched code. Signed-off-by: Petr Mladek --- lib/livepatch/test_klp_shadow_vars.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/lib/livepatch/test_klp_shadow_vars.c b/lib/livepatch/test_klp_shadow_vars.c index f5441c193166..fe5c413efe96 100644 --- a/lib/livepatch/test_klp_shadow_vars.c +++ b/lib/livepatch/test_klp_shadow_vars.c @@ -154,22 +154,37 @@ static int test_klp_shadow_vars_init(void) * Allocate a few shadow variables with different and . */ sv1 = shadow_alloc(obj, id, size, gfp_flags, shadow_ctor, &var1); + if (!sv1) + return -ENOMEM; + sv2 = shadow_alloc(obj + 1, id, size, gfp_flags, shadow_ctor, &var2); + if (!sv2) + return -ENOMEM; + sv3 = shadow_alloc(obj, id + 1, size, gfp_flags, shadow_ctor, &var3); + if (!sv3) + return -ENOMEM; /* * Verify we can find our new shadow variables and that they point * to expected data. */ ret = shadow_get(obj, id); + if (!ret) + return -EINVAL; if (ret == sv1 && *sv1 == &var1) pr_info(" got expected PTR%d -> PTR%d result\n", ptr_id(sv1), ptr_id(*sv1)); + ret = shadow_get(obj + 1, id); + if (!ret) + return -EINVAL; if (ret == sv2 && *sv2 == &var2) pr_info(" got expected PTR%d -> PTR%d result\n", ptr_id(sv2), ptr_id(*sv2)); ret = shadow_get(obj, id + 1); + if (!ret) + return -EINVAL; if (ret == sv3 && *sv3 == &var3) pr_info(" got expected PTR%d -> PTR%d result\n", ptr_id(sv3), ptr_id(*sv3)); @@ -179,7 +194,12 @@ static int test_klp_shadow_vars_init(void) * The second invocation should return the same shadow var. */ sv4 = shadow_get_or_alloc(obj + 2, id, size, gfp_flags, shadow_ctor, &var4); + if (!sv4) + return -ENOMEM; + ret = shadow_get_or_alloc(obj + 2, id, size, gfp_flags, shadow_ctor, &var4); + if (!ret) + return -EINVAL; if (ret == sv4 && *sv4 == &var4) pr_info(" got expected PTR%d -> PTR%d result\n", ptr_id(sv4), ptr_id(*sv4)); @@ -207,6 +227,8 @@ static int test_klp_shadow_vars_init(void) * We should still find an variable. */ ret = shadow_get(obj, id + 1); + if (!ret) + return -EINVAL; if (ret == sv3 && *sv3 == &var3) pr_info(" got expected PTR%d -> PTR%d result\n", ptr_id(sv3), ptr_id(*sv3)); -- 2.13.7