Received: by 2002:a05:7412:3b8b:b0:fc:a2b0:25d7 with SMTP id nd11csp1347764rdb; Fri, 9 Feb 2024 20:41:18 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCX8Ht69GY7M8+uwSPZCrjjm43WXfjt0P1MSTGLXPOOoMqWD7McudLUsV18xrHUgLCTnxCrb31wWjHL4u91f5ddgs6CeYmrljAZeHJPqGg== X-Google-Smtp-Source: AGHT+IFOh+MwctbYVIbbPqjTscnYpv2LqRBNmzo2JFvSAOvDcvX5OSGQr7luspQD1GiaInETzGif X-Received: by 2002:a0c:aa1a:0:b0:68c:48f5:31da with SMTP id d26-20020a0caa1a000000b0068c48f531damr1073225qvb.34.1707540077985; Fri, 09 Feb 2024 20:41:17 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707540077; cv=pass; d=google.com; s=arc-20160816; b=mdSpnhpl38s4Iq2s3ZshT8OGKMiSK9gUlRdaBuonCOKOSjviSx6D7QJ/B5pTY2OzaD prUsXltnGIbYtSm7L0SD6ln8jaAxdqlkwm10odwCCNUwWaqeLxWmbq/22uz2tfZ6B19B MaWulDd1eaNjFVnAX/ivsIS7EuBmGJOCSdNIiFXmLYC/hdkxRgC3rM1hd29HCpFSWifE /AXHU/K0Lqy+2+ZwTeTmhGd2Nmdem9Bgl9EDjoMkDyHFxEvK0xRTT5c+cO26CDv3BN9J zFHvg2+ToHe49s1Eb9N5kf4DOU/VS6UcbgZ59mbz0CvPVJ/woFe9QfM6cx7XCCy0Qi2A fqCA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:list-unsubscribe:list-subscribe :list-id:precedence; bh=7ZCGeaOgFb/POuILW7FEZXlx24Rn7F7T3YvkEdqGZn8=; fh=VocTz0F98zrWKjNSu46bV/e0K1ZCBrdhP0kAApHxJVg=; b=eajQ/qdXZ3biEa3HR6+TrprOcE0tpCx7H0AeRMc6I3/ix99vva4qCcKqm/iXyen1hn c9T8afVx/G0PBjkafvPdpjK7LXpPwwR5kpcR8wqvcOWcy6JgKP0iv8EwcNYo4pekSqTi HNn9MG/U7fhduSrwUsGvqfttJE/TT6mavQQMkDHXJuRi4eJrN8FVpUMKWIAIgHuDrklu fBu9J7t1FI9AEN8IGMg72DJdE1ivgnjYhMGzr87Pu0zKIphZ/T8vpsSJ87VNFSuiyoLA tr5ZKQpCkWO7KhnvNUZMyFOkqVo79xEiMPlfbSR1LAekfoOTc39Re0JKGIvXM+ssJCVu n+8g==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-60280-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-60280-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org X-Forwarded-Encrypted: i=2; AJvYcCX5CSGL+3gS4dVUOZh1PTBem6YOpujS8lFhPG/H+JCXAuwJ69IsGqTf8yyKXJwRT5tQ57mTjeh6GJkt+BdnR2NoHAPCADS1YNwVct5iHQ== Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id hf4-20020a0562140e8400b0068c837a92d6si3669308qvb.6.2024.02.09.20.41.17 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Feb 2024 20:41:17 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-60280-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=gmail.com); spf=pass (google.com: domain of linux-kernel+bounces-60280-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-60280-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id BACBC1C21405 for ; Sat, 10 Feb 2024 04:41:17 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id F31D863BF; Sat, 10 Feb 2024 04:41:09 +0000 (UTC) Received: from mail-oo1-f49.google.com (mail-oo1-f49.google.com [209.85.161.49]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 108E223B0; Sat, 10 Feb 2024 04:41:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.161.49 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707540069; cv=none; b=IFJDMVeb0ZhfEBuoSTab1jL6m5ubHVwRM9ZeMFH0tuxieimNAF2T1qja2HBooOA2pqIMyxwHO5PiuYrsAzjzQolYhsWJOtr4ceczPWuMtcITTrJPJONDeeLwTOPw7zVGhhXltRs2U1/Xk5UR5ZmKAR/EslxeIeCip44OdnrIeqA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707540069; c=relaxed/simple; bh=glEKylj6hGoshCUUwb23D4sXjlqavO0BCnKPI92hvlo=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=ZCEIMa6XCzcnHRG+VhUzPHL128gxYnkrweMGD9TMt+ccMmTmQQ9q16jFt0ifj8ro9Qsl3apKhF9U8YzFiCbW2QblZ9WPCQfZvyNU69ZUvVNkBrLOt/02GzGQ2ZJKmwv1/AsnxPB8uZZMWZoxKghksbpKUwqBU57F1rCqVKuPIdc= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=kernel.org; spf=pass smtp.mailfrom=gmail.com; arc=none smtp.client-ip=209.85.161.49 Authentication-Results: smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=kernel.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-oo1-f49.google.com with SMTP id 006d021491bc7-59d11e0b9e1so917264eaf.1; Fri, 09 Feb 2024 20:41:07 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707540067; x=1708144867; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=7ZCGeaOgFb/POuILW7FEZXlx24Rn7F7T3YvkEdqGZn8=; b=A2xBbxwe0JMbk/DL93RM3i181nBltz7HDElkyCQv9wADRh9DATvoVHXwo+DPTem04g WfrXt6KtTUZjLgQtrvA2bzZpKyOX1G4bkFFGM4ci7vEXktiKbMYaGB5fvdLHN8s4TxS/ 2Wbkd/s2pAId+2Uo+xebvlLY5Kr8/IOx96IudN2VQuS0poJklyJioqhYaBHQHq3t8ZP8 sJksiH1clmXXBXlIsZxCe0b72AhIGApk0/MyzuJ5DShOqPwp3HTnFY9qeyRP2QX2krvI x2QS1/D+s4XwwFF3D/HBizx0J1ZSzyO7s6W3OKghXW2ICPBtu3Jek1y6oa4QSwSXYwRg Xdzw== X-Forwarded-Encrypted: i=1; AJvYcCXPjzNpcB+8g3XbMfvqPwkqGntGfMq0zKFnVlpgGYTp4ZJ4YMucbw4uwktmJvL89jzjqaewPmuWcH1wrw276R+9BB1jaBfiRN244a+X1lZc2Df0Hx9pfi9gx7LM1I+RMNeEWMceNxiq9QsfJ3zJuw== X-Gm-Message-State: AOJu0Yzu1hRnMQq/hG/vngozwGOUVFeoaGFdTTSUspVZC68BdFhaEFsL MGYu45VtC4rxTxiCRn+yszXhNPjH+e2xAWiM19G3Y9nP8T4VuqxDccUQp9+VaYhTwA53+O+bFUe zDIkWPxeOy9m8hQcHCaM+/aW80PQ= X-Received: by 2002:a05:6358:5985:b0:179:22c:4a4d with SMTP id c5-20020a056358598500b00179022c4a4dmr2150235rwf.22.1707540066959; Fri, 09 Feb 2024 20:41:06 -0800 (PST) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20240201001504.1348511-1-irogers@google.com> <20240201001504.1348511-6-irogers@google.com> In-Reply-To: <20240201001504.1348511-6-irogers@google.com> From: Namhyung Kim Date: Fri, 9 Feb 2024 20:40:54 -0800 Message-ID: Subject: Re: [PATCH v2 6/9] perf tests: Use scandirat for shell script finding To: Ian Rogers Cc: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Adrian Hunter , Nathan Chancellor , Nick Desaulniers , Tom Rix , Ravi Bangoria , James Clark , Kan Liang , John Garry , linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, llvm@lists.linux.dev Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Wed, Jan 31, 2024 at 4:15=E2=80=AFPM Ian Rogers wro= te: > > Avoid filename appending buffers by using openat, faccessat and > scandirat more widely. Turn the script's path back to a file name > using readlink from /proc//fd/. > > Read the script's description using api/io.h to avoid fdopen > conversions. Whilst reading perform additional sanity checks on the > script's contents. > > Signed-off-by: Ian Rogers > --- [SNIP] > -static const char *shell_test__description(char *description, size_t siz= e, > - const char *path, const char = *name) > +static char *shell_test__description(int dir_fd, const char *name) > { > - FILE *fp; > - char filename[PATH_MAX]; > - int ch; > + struct io io; > + char buf[128], desc[256]; > + int ch, pos =3D 0; > > - path__join(filename, sizeof(filename), path, name); > - fp =3D fopen(filename, "r"); > - if (!fp) > + io__init(&io, openat(dir_fd, name, O_RDONLY), buf, sizeof(buf)); > + if (io.fd < 0) > return NULL; > > /* Skip first line - should be #!/bin/sh Shebang */ > + if (io__get_char(&io) !=3D '#') > + goto err_out; > + if (io__get_char(&io) !=3D '!') > + goto err_out; > do { > - ch =3D fgetc(fp); > - } while (ch !=3D EOF && ch !=3D '\n'); > - > - description =3D fgets(description, size, fp); > - fclose(fp); > + ch =3D io__get_char(&io); > + if (ch < 0) > + goto err_out; > + } while (ch !=3D '\n'); > > - /* Assume first char on line is omment everything after that desc= */ > - return description ? strim(description + 1) : NULL; > + do { > + ch =3D io__get_char(&io); > + if (ch < 0) > + goto err_out; > + } while (ch =3D=3D '#' || isspace(ch)); > + while (ch > 0 && ch !=3D '\n') { > + desc[pos++] =3D ch; > + if (pos >=3D (int)sizeof(desc) - 1) Maybe (pos =3D=3D sizeof(desc) - 2) ? I'm not sure what happens if it has = a description longer than the buffer size. > + break; > + ch =3D io__get_char(&io); > + } > + while (pos > 0 && isspace(desc[--pos])) > + ; > + desc[++pos] =3D '\0'; Wouldn't it overflow the buffer? Thanks, Namhyung > + close(io.fd); > + return strdup(desc); > +err_out: > + close(io.fd); > + return NULL; > }