Skip to content

Commit 3231dd5

Browse files
vegardJonathan Corbet
authored andcommitted
docs: kernel_abi.py: fix command injection
The kernel-abi 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. Link: https://fosstodon.org/@jani/111676532203641247 Reported-by: Jani Nikula <jani.nikula@intel.com> Cc: stable@vger.kernel.org Signed-off-by: Vegard Nossum <vegard.nossum@oracle.com> Signed-off-by: Jonathan Corbet <corbet@lwn.net> Link: https://lore.kernel.org/r/20231231235959.3342928-2-vegard.nossum@oracle.com
1 parent 5889d6e commit 3231dd5

5 files changed

Lines changed: 14 additions & 50 deletions

File tree

Documentation/admin-guide/abi-obsolete.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ marked to be removed at some later point in time.
77
The description of the interface will document the reason why it is
88
obsolete and when it can be expected to be removed.
99

10-
.. kernel-abi:: $srctree/Documentation/ABI/obsolete
10+
.. kernel-abi:: ABI/obsolete
1111
:rst:
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
ABI removed symbols
22
===================
33

4-
.. kernel-abi:: $srctree/Documentation/ABI/removed
4+
.. kernel-abi:: ABI/removed
55
:rst:

Documentation/admin-guide/abi-stable.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ for at least 2 years.
1010
Most interfaces (like syscalls) are expected to never change and always
1111
be available.
1212

13-
.. kernel-abi:: $srctree/Documentation/ABI/stable
13+
.. kernel-abi:: ABI/stable
1414
:rst:

Documentation/admin-guide/abi-testing.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@ Programs that use these interfaces are strongly encouraged to add their
1616
name to the description of these interfaces, so that the kernel
1717
developers can easily notify them if any changes occur.
1818

19-
.. kernel-abi:: $srctree/Documentation/ABI/testing
19+
.. kernel-abi:: ABI/testing
2020
:rst:

Documentation/sphinx/kernel_abi.py

Lines changed: 10 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,6 @@
3939
import re
4040
import kernellog
4141

42-
from os import path
43-
4442
from docutils import nodes, statemachine
4543
from docutils.statemachine import ViewList
4644
from docutils.parsers.rst import directives, Directive
@@ -73,60 +71,26 @@ class KernelCmd(Directive):
7371
}
7472

7573
def run(self):
76-
7774
doc = self.state.document
7875
if not doc.settings.file_insertion_enabled:
7976
raise self.warning("docutils: file insertion disabled")
8077

81-
env = doc.settings.env
82-
cwd = path.dirname(doc.current_source)
83-
cmd = "get_abi.pl rest --enable-lineno --dir "
84-
cmd += self.arguments[0]
85-
86-
if 'rst' in self.options:
87-
cmd += " --rst-source"
78+
srctree = os.path.abspath(os.environ["srctree"])
8879

89-
srctree = path.abspath(os.environ["srctree"])
80+
args = [
81+
os.path.join(srctree, 'scripts/get_abi.pl'),
82+
'rest',
83+
'--enable-lineno',
84+
'--dir', os.path.join(srctree, 'Documentation', self.arguments[0]),
85+
]
9086

91-
fname = cmd
92-
93-
# extend PATH with $(srctree)/scripts
94-
path_env = os.pathsep.join([
95-
srctree + os.sep + "scripts",
96-
os.environ["PATH"]
97-
])
98-
shell_env = os.environ.copy()
99-
shell_env["PATH"] = path_env
100-
shell_env["srctree"] = srctree
87+
if 'rst' in self.options:
88+
args.append('--rst-source')
10189

102-
lines = self.runCmd(cmd, shell=True, cwd=cwd, env=shell_env)
90+
lines = subprocess.check_output(args, cwd=os.path.dirname(doc.current_source)).decode('utf-8')
10391
nodeList = self.nestedParse(lines, self.arguments[0])
10492
return nodeList
10593

106-
def runCmd(self, cmd, **kwargs):
107-
u"""Run command ``cmd`` and return its stdout as unicode."""
108-
109-
try:
110-
proc = subprocess.Popen(
111-
cmd
112-
, stdout = subprocess.PIPE
113-
, stderr = subprocess.PIPE
114-
, **kwargs
115-
)
116-
out, err = proc.communicate()
117-
118-
out, err = codecs.decode(out, 'utf-8'), codecs.decode(err, 'utf-8')
119-
120-
if proc.returncode != 0:
121-
raise self.severe(
122-
u"command '%s' failed with return code %d"
123-
% (cmd, proc.returncode)
124-
)
125-
except OSError as exc:
126-
raise self.severe(u"problems with '%s' directive: %s."
127-
% (self.name, ErrorString(exc)))
128-
return out
129-
13094
def nestedParse(self, lines, fname):
13195
env = self.state.document.settings.env
13296
content = ViewList()

0 commit comments

Comments
 (0)