From: Ian Rogers <irogers@google.com>
To: Peter Zijlstra <peterz@infradead.org>,
Ingo Molnar <mingo@redhat.com>,
Arnaldo Carvalho de Melo <acme@kernel.org>,
Mark Rutland <mark.rutland@arm.com>,
Alexander Shishkin <alexander.shishkin@linux.intel.com>,
Jiri Olsa <jolsa@kernel.org>, Namhyung Kim <namhyung@kernel.org>,
Nathan Chancellor <nathan@kernel.org>,
Nick Desaulniers <ndesaulniers@google.com>,
Tom Rix <trix@redhat.com>, Nicolas Schier <nicolas@fjasle.eu>,
Masahiro Yamada <masahiroy@kernel.org>,
Athira Rajeev <atrajeev@linux.vnet.ibm.com>,
Christy Lee <christylee@fb.com>,
Andrii Nakryiko <andrii@kernel.org>,
Ravi Bangoria <ravi.bangoria@amd.com>,
Leo Yan <leo.yan@linaro.org>,
Yang Jihong <yangjihong1@huawei.com>,
Qi Liu <liuqi115@huawei.com>, James Clark <james.clark@arm.com>,
Adrian Hunter <adrian.hunter@intel.com>,
"Masami Hiramatsu (Google)" <mhiramat@kernel.org>,
Kan Liang <kan.liang@linux.intel.com>,
Sean Christopherson <seanjc@google.com>,
Zhengjun Xing <zhengjun.xing@linux.intel.com>,
Rob Herring <robh@kernel.org>, Xin Gao <gaoxin@cdjrlc.com>,
Zechuan Chen <chenzechuan1@huawei.com>,
Jason Wang <wangborong@cdjrlc.com>,
Christophe JAILLET <christophe.jaillet@wanadoo.fr>,
Stephane Eranian <eranian@google.com>,
German Gomez <german.gomez@arm.com>,
linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org,
bpf@vger.kernel.org, llvm@lists.linux.dev
Cc: Ian Rogers <irogers@google.com>
Subject: [PATCH v1 1/7] perf llvm: Fix inadvertent file creation
Date: Tue, 10 Jan 2023 14:19:57 -0800 [thread overview]
Message-ID: <20230110222003.1591436-2-irogers@google.com> (raw)
In-Reply-To: <20230110222003.1591436-1-irogers@google.com>
The LLVM template is first echo-ed into command_out and then
command_out executed. The echo surrounds the template with double
quotes, however, the template itself may contain quotes. This is
generally innocuous but in tools/perf/tests/bpf-script-test-prologue.c
we see:
...
SEC("func=null_lseek file->f_mode offset orig")
...
where the first double quote ends the double quote of the echo, then
the > redirects output into a file called f_mode.
To avoid this inadvertent behavior substitute redirects and similar
characters to be ASCII control codes, then substitute the output in
the echo back again.
Fixes: 5eab5a7ee032 ("perf llvm: Display eBPF compiling command in debug output")
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/util/llvm-utils.c | 25 ++++++++++++++++++++++++-
1 file changed, 24 insertions(+), 1 deletion(-)
diff --git a/tools/perf/util/llvm-utils.c b/tools/perf/util/llvm-utils.c
index 650ffe336f3a..4e8e243a6e4b 100644
--- a/tools/perf/util/llvm-utils.c
+++ b/tools/perf/util/llvm-utils.c
@@ -531,14 +531,37 @@ int llvm__compile_bpf(const char *path, void **p_obj_buf,
pr_debug("llvm compiling command template: %s\n", template);
+ /*
+ * Below, substitute control characters for values that can cause the
+ * echo to misbehave, then substitute the values back.
+ */
err = -ENOMEM;
- if (asprintf(&command_echo, "echo -n \"%s\"", template) < 0)
+ if (asprintf(&command_echo, "echo -n \a%s\a", template) < 0)
goto errout;
+#define SWAP_CHAR(a, b) do { if (*p == a) *p = b; } while (0)
+ for (char *p = command_echo; *p; p++) {
+ SWAP_CHAR('<', '\001');
+ SWAP_CHAR('>', '\002');
+ SWAP_CHAR('"', '\003');
+ SWAP_CHAR('\'', '\004');
+ SWAP_CHAR('|', '\005');
+ SWAP_CHAR('&', '\006');
+ SWAP_CHAR('\a', '"');
+ }
err = read_from_pipe(command_echo, (void **) &command_out, NULL);
if (err)
goto errout;
+ for (char *p = command_out; *p; p++) {
+ SWAP_CHAR('\001', '<');
+ SWAP_CHAR('\002', '>');
+ SWAP_CHAR('\003', '"');
+ SWAP_CHAR('\004', '\'');
+ SWAP_CHAR('\005', '|');
+ SWAP_CHAR('\006', '&');
+ }
+#undef SWAP_CHAR
pr_debug("llvm compiling command : %s\n", command_out);
err = read_from_pipe(template, &obj_buf, &obj_buf_sz);
--
2.39.0.314.g84b9a713c41-goog
next prev parent reply other threads:[~2023-01-10 22:20 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-01-10 22:19 [PATCH v1 0/7] Add and use run_command_strbuf Ian Rogers
2023-01-10 22:19 ` Ian Rogers [this message]
2023-01-10 22:19 ` [PATCH v1 2/7] tools lib: Move strbuf to libapi Ian Rogers
2023-01-10 22:19 ` [PATCH v1 3/7] tools lib subcmd: Add run_command_strbuf Ian Rogers
2023-01-10 22:20 ` [PATCH v1 4/7] tools lib api: Minor strbuf_read improvements Ian Rogers
2023-01-10 22:20 ` [PATCH v1 5/7] tools lib api: Tweak strbuf allocation size computation Ian Rogers
2023-01-10 22:20 ` [PATCH v1 6/7] perf help: Use run_command_strbuf Ian Rogers
2023-01-10 22:20 ` [PATCH v1 7/7] perf llvm: Remove read_from_pipe Ian Rogers
2023-01-19 16:05 ` [PATCH v1 0/7] Add and use run_command_strbuf Ian Rogers
2023-01-19 16:28 ` Arnaldo Carvalho de Melo
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230110222003.1591436-2-irogers@google.com \
--to=irogers@google.com \
--cc=acme@kernel.org \
--cc=adrian.hunter@intel.com \
--cc=alexander.shishkin@linux.intel.com \
--cc=andrii@kernel.org \
--cc=atrajeev@linux.vnet.ibm.com \
--cc=bpf@vger.kernel.org \
--cc=chenzechuan1@huawei.com \
--cc=christophe.jaillet@wanadoo.fr \
--cc=christylee@fb.com \
--cc=eranian@google.com \
--cc=gaoxin@cdjrlc.com \
--cc=german.gomez@arm.com \
--cc=james.clark@arm.com \
--cc=jolsa@kernel.org \
--cc=kan.liang@linux.intel.com \
--cc=leo.yan@linaro.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-perf-users@vger.kernel.org \
--cc=liuqi115@huawei.com \
--cc=llvm@lists.linux.dev \
--cc=mark.rutland@arm.com \
--cc=masahiroy@kernel.org \
--cc=mhiramat@kernel.org \
--cc=mingo@redhat.com \
--cc=namhyung@kernel.org \
--cc=nathan@kernel.org \
--cc=ndesaulniers@google.com \
--cc=nicolas@fjasle.eu \
--cc=peterz@infradead.org \
--cc=ravi.bangoria@amd.com \
--cc=robh@kernel.org \
--cc=seanjc@google.com \
--cc=trix@redhat.com \
--cc=wangborong@cdjrlc.com \
--cc=yangjihong1@huawei.com \
--cc=zhengjun.xing@linux.intel.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).