Skip to content

Commit

Permalink
docs: kernel_feat.py: fix potential command injection
Browse files Browse the repository at this point in the history
[ Upstream commit c48a7c4 ]

The kernel-feat directive passes its argument straight to the shell.
This is unfortunate and unnecessary.

Let's always use paths relative to $srctree/Documentation/ and use
subprocess.check_call() instead of subprocess.Popen(shell=True).

This also makes the code shorter.

This is analogous to commit 3231dd5 ("docs: kernel_abi.py: fix
command injection") where we did exactly the same thing for
kernel_abi.py, somehow I completely missed this one.

Link: https://fosstodon.org/@jani/111676532203641247
Reported-by: Jani Nikula <jani.nikula@intel.com>
Signed-off-by: Vegard Nossum <vegard.nossum@oracle.com>
Cc: stable@vger.kernel.org
Signed-off-by: Jonathan Corbet <corbet@lwn.net>
Link: https://lore.kernel.org/r/20240110174758.3680506-1-vegard.nossum@oracle.com
Signed-off-by: Sasha Levin <sashal@kernel.org>
  • Loading branch information
vegard authored and gregkh committed Feb 1, 2024
1 parent db6e7e5 commit e961f8c
Show file tree
Hide file tree
Showing 20 changed files with 30 additions and 63 deletions.
2 changes: 1 addition & 1 deletion Documentation/admin-guide/features.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
.. SPDX-License-Identifier: GPL-2.0
.. kernel-feat:: $srctree/Documentation/features
.. kernel-feat:: features
2 changes: 1 addition & 1 deletion Documentation/arch/arc/features.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
.. SPDX-License-Identifier: GPL-2.0
.. kernel-feat:: $srctree/Documentation/features arc
.. kernel-feat:: features arc
2 changes: 1 addition & 1 deletion Documentation/arch/arm/features.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
.. SPDX-License-Identifier: GPL-2.0
.. kernel-feat:: $srctree/Documentation/features arm
.. kernel-feat:: features arm
2 changes: 1 addition & 1 deletion Documentation/arch/arm64/features.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
.. SPDX-License-Identifier: GPL-2.0
.. kernel-feat:: $srctree/Documentation/features arm64
.. kernel-feat:: features arm64
2 changes: 1 addition & 1 deletion Documentation/arch/loongarch/features.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
.. SPDX-License-Identifier: GPL-2.0
.. kernel-feat:: $srctree/Documentation/features loongarch
.. kernel-feat:: features loongarch
2 changes: 1 addition & 1 deletion Documentation/arch/m68k/features.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
.. SPDX-License-Identifier: GPL-2.0
.. kernel-feat:: $srctree/Documentation/features m68k
.. kernel-feat:: features m68k
2 changes: 1 addition & 1 deletion Documentation/arch/mips/features.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
.. SPDX-License-Identifier: GPL-2.0
.. kernel-feat:: $srctree/Documentation/features mips
.. kernel-feat:: features mips
2 changes: 1 addition & 1 deletion Documentation/arch/nios2/features.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
.. SPDX-License-Identifier: GPL-2.0
.. kernel-feat:: $srctree/Documentation/features nios2
.. kernel-feat:: features nios2
2 changes: 1 addition & 1 deletion Documentation/arch/openrisc/features.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
.. SPDX-License-Identifier: GPL-2.0
.. kernel-feat:: $srctree/Documentation/features openrisc
.. kernel-feat:: features openrisc
2 changes: 1 addition & 1 deletion Documentation/arch/parisc/features.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
.. SPDX-License-Identifier: GPL-2.0
.. kernel-feat:: $srctree/Documentation/features parisc
.. kernel-feat:: features parisc
2 changes: 1 addition & 1 deletion Documentation/arch/s390/features.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
.. SPDX-License-Identifier: GPL-2.0
.. kernel-feat:: $srctree/Documentation/features s390
.. kernel-feat:: features s390
2 changes: 1 addition & 1 deletion Documentation/arch/sh/features.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
.. SPDX-License-Identifier: GPL-2.0
.. kernel-feat:: $srctree/Documentation/features sh
.. kernel-feat:: features sh
2 changes: 1 addition & 1 deletion Documentation/arch/sparc/features.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
.. SPDX-License-Identifier: GPL-2.0
.. kernel-feat:: $srctree/Documentation/features sparc
.. kernel-feat:: features sparc
2 changes: 1 addition & 1 deletion Documentation/arch/x86/features.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
.. SPDX-License-Identifier: GPL-2.0
.. kernel-feat:: $srctree/Documentation/features x86
.. kernel-feat:: features x86
2 changes: 1 addition & 1 deletion Documentation/arch/xtensa/features.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
.. SPDX-License-Identifier: GPL-2.0
.. kernel-feat:: $srctree/Documentation/features xtensa
.. kernel-feat:: features xtensa
2 changes: 1 addition & 1 deletion Documentation/powerpc/features.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
.. SPDX-License-Identifier: GPL-2.0
.. kernel-feat:: $srctree/Documentation/features powerpc
.. kernel-feat:: features powerpc
2 changes: 1 addition & 1 deletion Documentation/riscv/features.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
.. SPDX-License-Identifier: GPL-2.0
.. kernel-feat:: $srctree/Documentation/features riscv
.. kernel-feat:: features riscv
55 changes: 11 additions & 44 deletions Documentation/sphinx/kernel_feat.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@
import subprocess
import sys

from os import path

from docutils import nodes, statemachine
from docutils.statemachine import ViewList
from docutils.parsers.rst import directives, Directive
Expand Down Expand Up @@ -76,33 +74,26 @@ def warn(self, message, **replace):
self.state.document.settings.env.app.warn(message, prefix="")

def run(self):

doc = self.state.document
if not doc.settings.file_insertion_enabled:
raise self.warning("docutils: file insertion disabled")

env = doc.settings.env
cwd = path.dirname(doc.current_source)
cmd = "get_feat.pl rest --enable-fname --dir "
cmd += self.arguments[0]

if len(self.arguments) > 1:
cmd += " --arch " + self.arguments[1]

srctree = path.abspath(os.environ["srctree"])
srctree = os.path.abspath(os.environ["srctree"])

fname = cmd
args = [
os.path.join(srctree, 'scripts/get_feat.pl'),
'rest',
'--enable-fname',
'--dir',
os.path.join(srctree, 'Documentation', self.arguments[0]),
]

# extend PATH with $(srctree)/scripts
path_env = os.pathsep.join([
srctree + os.sep + "scripts",
os.environ["PATH"]
])
shell_env = os.environ.copy()
shell_env["PATH"] = path_env
shell_env["srctree"] = srctree
if len(self.arguments) > 1:
args.extend(['--arch', self.arguments[1]])

lines = self.runCmd(cmd, shell=True, cwd=cwd, env=shell_env)
lines = subprocess.check_output(args, cwd=os.path.dirname(doc.current_source)).decode('utf-8')

line_regex = re.compile("^\.\. FILE (\S+)$")

Expand All @@ -121,30 +112,6 @@ def run(self):
nodeList = self.nestedParse(out_lines, fname)
return nodeList

def runCmd(self, cmd, **kwargs):
u"""Run command ``cmd`` and return its stdout as unicode."""

try:
proc = subprocess.Popen(
cmd
, stdout = subprocess.PIPE
, stderr = subprocess.PIPE
, **kwargs
)
out, err = proc.communicate()

out, err = codecs.decode(out, 'utf-8'), codecs.decode(err, 'utf-8')

if proc.returncode != 0:
raise self.severe(
u"command '%s' failed with return code %d"
% (cmd, proc.returncode)
)
except OSError as exc:
raise self.severe(u"problems with '%s' directive: %s."
% (self.name, ErrorString(exc)))
return out

def nestedParse(self, lines, fname):
content = ViewList()
node = nodes.section()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
:Original: Documentation/arch/loongarch/features.rst
:Translator: Huacai Chen <chenhuacai@loongson.cn>

.. kernel-feat:: $srctree/Documentation/features loongarch
.. kernel-feat:: features loongarch
2 changes: 1 addition & 1 deletion Documentation/translations/zh_CN/arch/mips/features.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@

.. _cn_features:

.. kernel-feat:: $srctree/Documentation/features mips
.. kernel-feat:: features mips

0 comments on commit e961f8c

Please sign in to comment.