Received: by 10.223.185.116 with SMTP id b49csp5532914wrg; Tue, 27 Feb 2018 15:17:57 -0800 (PST) X-Google-Smtp-Source: AH8x227sPIMOlYQDGgYEZelb4PHuVrHn9Fd9oOZ4wEV6y2/k0lS51ByK1LiFmWzQV03I7BPqAWqJ X-Received: by 10.101.89.74 with SMTP id g10mr12541056pgu.415.1519773477746; Tue, 27 Feb 2018 15:17:57 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1519773477; cv=none; d=google.com; s=arc-20160816; b=n5sgDzmxeTcdVtJiVAub5KyGwPpeUzEHPpbLcCj9MJqcSWIwll/r9/4V8eM6iAVES8 dvalZNxB9WcqDc/6mskQ98mNomK8bcIG4Jqz7Ett9VUJ6499Bx2TVUt4tB0Vt6qfd7ta soAxYk/sWGa6WPg41Ip6pF11xOHmkiHapE6Oict5Sg3yM/C1CYE2lrNq23m3DzTZNztc MTJXBem8chfLbkJ92Leuc4pee4rLqGkAPHEPFqNDt6ab8UgbsQYXQ68giZuTtCEA36zP d2sJ8w2h0D27TbTvomwA+/sRoOyO57ERgBoCxOFfhYRdhJ70VUZtBgAv2GJ9EgJXWRsR 9kMA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :references:in-reply-to:mime-version:dkim-signature:dkim-signature :arc-authentication-results; bh=taIRZAeROC3eRXJ+d32kjjuo4QkvEKa63who4S2m6kY=; b=n563PPRRpOSMELeJVi5Jjq6Sg8VrnH69kzFWzaKYav4b1vSkCERNIjfV2ieqBbncY9 RVm7ua/qpUHEuxZzk6BYVg1Q6iXHlQrfrlU0Q9FY9Ob2G2y/RE8klbQ9xX6zK0RSjttV xBcAt5Wf/SljeNZl6ELIugs4fuJ2aPWplQiQpgzAZaukB6qoDSowMyGx1CSSMi2UNDq1 BWmZkL+72fCqqc0Ne2uAHwNctGdTWBOq0Vfm0OMxEysW4Xp2Mf3QEOmP7l0LYijARMiF HmCvct6CVQwjhdlABG1o3Kav+Y8jkIy+vU8dvuInvzAsTzSIF4fLFw5RvDQ0FsuwOcAh +23A== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@google.com header.s=20161025 header.b=bWTJfnJZ; dkim=fail header.i=@chromium.org header.s=google header.b=QMSAWRCQ; 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id k185si162375pfc.277.2018.02.27.15.17.42; Tue, 27 Feb 2018 15:17:57 -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; dkim=fail header.i=@google.com header.s=20161025 header.b=bWTJfnJZ; dkim=fail header.i=@chromium.org header.s=google header.b=QMSAWRCQ; 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752058AbeB0XRD (ORCPT + 99 others); Tue, 27 Feb 2018 18:17:03 -0500 Received: from mail-vk0-f67.google.com ([209.85.213.67]:36024 "EHLO mail-vk0-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751835AbeB0XRA (ORCPT ); Tue, 27 Feb 2018 18:17:00 -0500 Received: by mail-vk0-f67.google.com with SMTP id q7so362065vkh.3 for ; Tue, 27 Feb 2018 15:17:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:sender:in-reply-to:references:from:date:message-id :subject:to:cc; bh=taIRZAeROC3eRXJ+d32kjjuo4QkvEKa63who4S2m6kY=; b=bWTJfnJZGR+R5YztNQouyjKxLZYjjc2SXC18MTkU1ec2/FAV03OvYWozNFD25II58Y c5pj/6C5xo0gnAj0x619gK2MwnZZWSrROcgomSC7PUWuAp89jCLq9STewaQJ6q0Tc2nu /Pg1MZ6iRyNcQCLfkxgwL3qG94aI3RSp28aJz8qTn7ekO9cIUKueCPBuZBoEr5CJnwTK P7JL1g8j22n06JQ7b5piCktWh14VagS0QvgV+oLRSputusBBlGZo2CpyBQ1ljrKmCm/G MqVEqDs2qKFsqUnaaWtRPXMADuH1QpqnwP61Sl0MCPsL3vHtQ7kGfWgFPeB50kAD55mw 8qKw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=mime-version:sender:in-reply-to:references:from:date:message-id :subject:to:cc; bh=taIRZAeROC3eRXJ+d32kjjuo4QkvEKa63who4S2m6kY=; b=QMSAWRCQ3jq2FUb7QNTK7ycx1tfnHLxoyYWYVc+WiUyRzCWoF6dt4Ayuyn39//2K18 RtAZaLe4o64sHCy97usOUyaW5FufW7R4DSunNlvDCvscvm/h4Jfa310ERzqTvsdK5WvN 4A8EKECevtBmtxDGKex5xN/W5KS8UZ9QHaqH8= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:sender:in-reply-to:references:from :date:message-id:subject:to:cc; bh=taIRZAeROC3eRXJ+d32kjjuo4QkvEKa63who4S2m6kY=; b=pT9RphuvTyScd+bveKMAW9cWr9Tfm/udzYkKy1rzngdM9L5QaJEy2lNocsBXXSo5XW vUVoWIewcnx2OWAkq6lZEj4XZ4Sse492KDmQt8im7F5grxfPsPAbPY+laG6RB7bi8k9h NQXHKW8+tb1X6eoe5pTaEVByk/CXTjwazXMr5Gr83bLfzjlXki8u92CERWxV+gPoJNgq 7bJxBLhLReMi3CHRC7ypjOQPFEkgAmPqCeb/2//bCR/8mhyofqL4q9V4fsBOJrxeVBZr ychRRSuFviyiJEXwWldZj8WpaRAFN6gcA/m9MrzH612W8aGQTtH1WMAnjIYBe9WWyrOf RKjQ== X-Gm-Message-State: APf1xPA5tb0MgdyQv99FpaC1COlJ+X3lAVoQQZJsKlsUUNIxBO7bfWrx 6GPetdMPtTXqciboi0vK8LTUFVJPXUN9wE6UQXETaw== X-Received: by 10.31.230.132 with SMTP id d126mr11849842vkh.123.1519773419519; Tue, 27 Feb 2018 15:16:59 -0800 (PST) MIME-Version: 1.0 Received: by 10.31.242.140 with HTTP; Tue, 27 Feb 2018 15:16:58 -0800 (PST) In-Reply-To: <20180224024613.24078-11-mcgrof@kernel.org> References: <20180224024613.24078-1-mcgrof@kernel.org> <20180224024613.24078-11-mcgrof@kernel.org> From: Kees Cook Date: Tue, 27 Feb 2018 15:16:58 -0800 X-Google-Sender-Auth: kOZC0CXulIPuPz22ukQKXq4UXcM Message-ID: Subject: Re: [PATCH v2 10/11] test_firmware: add a library for shared helpers To: "Luis R. Rodriguez" Cc: Greg KH , Andrew Morton , Shuah Khan , Martin Fuzzey , Mimi Zohar , David Howells , pali.rohar@gmail.com, Takashi Iwai , arend.vanspriel@broadcom.com, =?UTF-8?B?UmFmYcWCIE1pxYJlY2tp?= , nbroeking@me.com, Vikram Mulukutla , stephen.boyd@linaro.org, Mark Brown , Dmitry Torokhov , David Woodhouse , Linus Torvalds , Abhay_Salunke@dell.com, bjorn.andersson@linaro.org, jewalt@lgsinnovations.com, LKML , "linux-fsdevel@vger.kernel.org" Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Feb 23, 2018 at 6:46 PM, Luis R. Rodriguez wrote: > Both fw_fallback.sh and fw_filesystem.sh share a common set of > boiler plate setup and tests. We can share these in a common place. > While at it, move both test to use /bin/bash. > > Signed-off-by: Luis R. Rodriguez > --- > tools/testing/selftests/firmware/fw_fallback.sh | 69 ++----------- > tools/testing/selftests/firmware/fw_filesystem.sh | 61 ++--------- > tools/testing/selftests/firmware/fw_lib.sh | 120 ++++++++++++++++++++++ Why not initially create this fw_lib.sh instead of moving it later? -Kees > 3 files changed, 137 insertions(+), 113 deletions(-) > create mode 100755 tools/testing/selftests/firmware/fw_lib.sh > > diff --git a/tools/testing/selftests/firmware/fw_fallback.sh b/tools/testing/selftests/firmware/fw_fallback.sh > index 40b6c1d3e832..0db1c09a6bcc 100755 > --- a/tools/testing/selftests/firmware/fw_fallback.sh > +++ b/tools/testing/selftests/firmware/fw_fallback.sh > @@ -1,4 +1,4 @@ > -#!/bin/sh > +#!/bin/bash > # SPDX-License-Identifier: GPL-2.0 > # This validates that the kernel will fall back to using the fallback mechanism > # to load firmware it can't find on disk itself. We must request a firmware > @@ -6,68 +6,17 @@ > # won't find so that we can do the load ourself manually. > set -e > > -PROC_CONFIG="/proc/config.gz" > +TEST_REQS_FW_SYSFS_FALLBACK="yes" > +TEST_REQS_FW_SET_CUSTOM_PATH="no" > TEST_DIR=$(dirname $0) > +source $TEST_DIR/fw_lib.sh > > -modprobe test_firmware > -if [ ! -f $PROC_CONFIG ]; then > - if modprobe configs 2>/dev/null; then > - echo "Loaded configs module" > - if [ ! -f $PROC_CONFIG ]; then > - echo "You must have the following enabled in your kernel:" >&2 > - cat $TEST_DIR/config >&2 > - echo "Resorting to old heuristics" >&2 > - fi > - else > - echo "Failed to load configs module, using old heuristics" >&2 > - fi > -fi > - > -kconfig_has() > -{ > - if [ -f $PROC_CONFIG ]; then > - if zgrep -q $1 $PROC_CONFIG 2>/dev/null; then > - echo "yes" > - else > - echo "no" > - fi > - else > - # We currently don't have easy heuristics to infer this > - # so best we can do is just try to use the kernel assuming > - # you had enabled it. This matches the old behaviour. > - if [ "$1" = "CONFIG_FW_LOADER_USER_HELPER_FALLBACK=y" ]; then > - echo "yes" > - elif [ "$1" = "CONFIG_FW_LOADER_USER_HELPER=y" ]; then > - if [ -d /sys/class/firmware/ ]; then > - echo yes > - else > - echo no > - fi > - fi > - fi > -} > - > -DIR=/sys/devices/virtual/misc/test_firmware > - > -HAS_FW_LOADER_USER_HELPER=$(kconfig_has CONFIG_FW_LOADER_USER_HELPER=y) > -HAS_FW_LOADER_USER_HELPER_FALLBACK=$(kconfig_has CONFIG_FW_LOADER_USER_HELPER_FALLBACK=y) > - > -if [ "$HAS_FW_LOADER_USER_HELPER" = "yes" ]; then > - OLD_TIMEOUT=$(cat /sys/class/firmware/timeout) > -else > - echo "usermode helper disabled so ignoring test" > - exit 0 > -fi > - > -FWPATH=$(mktemp -d) > -FW="$FWPATH/test-firmware.bin" > +check_mods > +check_setup > +verify_reqs > +setup_tmp_file > > -test_finish() > -{ > - echo "$OLD_TIMEOUT" >/sys/class/firmware/timeout > - rm -f "$FW" > - rmdir "$FWPATH" > -} > +trap "test_finish" EXIT > > load_fw() > { > diff --git a/tools/testing/selftests/firmware/fw_filesystem.sh b/tools/testing/selftests/firmware/fw_filesystem.sh > index f9508e1a4058..7f47877fa7fa 100755 > --- a/tools/testing/selftests/firmware/fw_filesystem.sh > +++ b/tools/testing/selftests/firmware/fw_filesystem.sh > @@ -1,4 +1,4 @@ > -#!/bin/sh > +#!/bin/bash > # SPDX-License-Identifier: GPL-2.0 > # This validates that the kernel will load firmware out of its list of > # firmware locations on disk. Since the user helper does similar work, > @@ -6,52 +6,15 @@ > # know so we can be sure we're not accidentally testing the user helper. > set -e > > -DIR=/sys/devices/virtual/misc/test_firmware > +TEST_REQS_FW_SYSFS_FALLBACK="no" > +TEST_REQS_FW_SET_CUSTOM_PATH="yes" > TEST_DIR=$(dirname $0) > +source $TEST_DIR/fw_lib.sh > > -test_modprobe() > -{ > - if [ ! -d $DIR ]; then > - echo "$0: $DIR not present" > - echo "You must have the following enabled in your kernel:" > - cat $TEST_DIR/config > - exit 1 > - fi > -} > - > -trap "test_modprobe" EXIT > - > -if [ ! -d $DIR ]; then > - modprobe test_firmware > -fi > - > -# CONFIG_FW_LOADER_USER_HELPER has a sysfs class under /sys/class/firmware/ > -# These days most distros enable CONFIG_FW_LOADER_USER_HELPER but disable > -# CONFIG_FW_LOADER_USER_HELPER_FALLBACK. We use /sys/class/firmware/ as an > -# indicator for CONFIG_FW_LOADER_USER_HELPER. > -HAS_FW_LOADER_USER_HELPER=$(if [ -d /sys/class/firmware/ ]; then echo yes; else echo no; fi) > - > -if [ "$HAS_FW_LOADER_USER_HELPER" = "yes" ]; then > - OLD_TIMEOUT=$(cat /sys/class/firmware/timeout) > -fi > - > -OLD_FWPATH=$(cat /sys/module/firmware_class/parameters/path) > - > -FWPATH=$(mktemp -d) > -FW="$FWPATH/test-firmware.bin" > - > -test_finish() > -{ > - if [ "$HAS_FW_LOADER_USER_HELPER" = "yes" ]; then > - echo "$OLD_TIMEOUT" >/sys/class/firmware/timeout > - fi > - if [ "$OLD_FWPATH" = "" ]; then > - OLD_FWPATH=" " > - fi > - echo -n "$OLD_FWPATH" >/sys/module/firmware_class/parameters/path > - rm -f "$FW" > - rmdir "$FWPATH" > -} > +check_mods > +check_setup > +verify_reqs > +setup_tmp_file > > trap "test_finish" EXIT > > @@ -60,14 +23,6 @@ if [ "$HAS_FW_LOADER_USER_HELPER" = "yes" ]; then > echo 1 >/sys/class/firmware/timeout > fi > > -# Set the kernel search path. > -echo -n "$FWPATH" >/sys/module/firmware_class/parameters/path > - > -# This is an unlikely real-world firmware content. :) > -echo "ABCD0123" >"$FW" > - > -NAME=$(basename "$FW") > - > if printf '\000' >"$DIR"/trigger_request 2> /dev/null; then > echo "$0: empty filename should not succeed" >&2 > exit 1 > diff --git a/tools/testing/selftests/firmware/fw_lib.sh b/tools/testing/selftests/firmware/fw_lib.sh > new file mode 100755 > index 000000000000..0702dbf0f06b > --- /dev/null > +++ b/tools/testing/selftests/firmware/fw_lib.sh > @@ -0,0 +1,120 @@ > +#!/bin/sh > +# SPDX-License-Identifier: GPL-2.0 > + > +# Library of helpers for test scripts. > +set -e > + > +DIR=/sys/devices/virtual/misc/test_firmware > + > +PROC_CONFIG="/proc/config.gz" > +TEST_DIR=$(dirname $0) > + > +print_reqs_exit() > +{ > + echo "You must have the following enabled in your kernel:" >&2 > + cat $TEST_DIR/config >&2 > + exit 1 > +} > + > +test_modprobe() > +{ > + if [ ! -d $DIR ]; then > + print_reqs_exit > + fi > +} > + > +check_mods() > +{ > + trap "test_modprobe" EXIT > + if [ ! -d $DIR ]; then > + modprobe test_firmware > + fi > + if [ ! -f $PROC_CONFIG ]; then > + if modprobe configs 2>/dev/null; then > + echo "Loaded configs module" > + if [ ! -f $PROC_CONFIG ]; then > + echo "You must have the following enabled in your kernel:" >&2 > + cat $TEST_DIR/config >&2 > + echo "Resorting to old heuristics" >&2 > + fi > + else > + echo "Failed to load configs module, using old heuristics" >&2 > + fi > + fi > +} > + > +check_setup() > +{ > + HAS_FW_LOADER_USER_HELPER=$(kconfig_has CONFIG_FW_LOADER_USER_HELPER=y) > + HAS_FW_LOADER_USER_HELPER_FALLBACK=$(kconfig_has CONFIG_FW_LOADER_USER_HELPER_FALLBACK=y) > + > + if [ "$HAS_FW_LOADER_USER_HELPER" = "yes" ]; then > + OLD_TIMEOUT=$(cat /sys/class/firmware/timeout) > + fi > + > + OLD_FWPATH=$(cat /sys/module/firmware_class/parameters/path) > +} > + > +verify_reqs() > +{ > + if [ "$TEST_REQS_FW_SYSFS_FALLBACK" = "yes" ]; then > + if [ ! "$HAS_FW_LOADER_USER_HELPER" = "yes" ]; then > + echo "usermode helper disabled so ignoring test" > + exit 1 > + fi > + fi > +} > + > +setup_tmp_file() > +{ > + FWPATH=$(mktemp -d) > + FW="$FWPATH/test-firmware.bin" > + echo "ABCD0123" >"$FW" > + NAME=$(basename "$FW") > + if [ "$TEST_REQS_FW_SET_CUSTOM_PATH" = "yes" ]; then > + echo -n "$FWPATH" >/sys/module/firmware_class/parameters/path > + fi > +} > + > +test_finish() > +{ > + if [ "$HAS_FW_LOADER_USER_HELPER" = "yes" ]; then > + echo "$OLD_TIMEOUT" >/sys/class/firmware/timeout > + fi > + if [ "$OLD_FWPATH" = "" ]; then > + OLD_FWPATH=" " > + fi > + if [ "$TEST_REQS_FW_SET_CUSTOM_PATH" = "yes" ]; then > + echo -n "$OLD_FWPATH" >/sys/module/firmware_class/parameters/path > + fi > + if [ -f $FW ]; then > + rm -f "$FW" > + fi > + if [ -d $FWPATH ]; then > + rm -rf "$FWPATH" > + fi > +} > + > +kconfig_has() > +{ > + if [ -f $PROC_CONFIG ]; then > + if zgrep -q $1 $PROC_CONFIG 2>/dev/null; then > + echo "yes" > + else > + echo "no" > + fi > + else > + # We currently don't have easy heuristics to infer this > + # so best we can do is just try to use the kernel assuming > + # you had enabled it. This matches the old behaviour. > + if [ "$1" = "CONFIG_FW_LOADER_USER_HELPER_FALLBACK=y" ]; then > + echo "yes" > + elif [ "$1" = "CONFIG_FW_LOADER_USER_HELPER=y" ]; then > + if [ -d /sys/class/firmware/ ]; then > + echo yes > + else > + echo no > + fi > + fi > + fi > +} > -- > 2.16.2 > -- Kees Cook Pixel Security