Skip to content

Commit c23cc5f

Browse files
authored
Merge pull request #84371 from edymtt/edymtt/harden-python-scripts
Reduce surface for command injection in some Python scripts
2 parents 4da340a + a10313e commit c23cc5f

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

utils/swift-darwin-postprocess.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,18 @@
1313
get_task_allow_plist = os.path.join(utils, 'get-task-allow.plist')
1414

1515

16+
def file_path(string):
17+
if os.path.isfile(string):
18+
return string
19+
else:
20+
raise argparse.ArgumentTypeError(f"{string} is not a valid path")
21+
22+
1623
def main(arguments):
1724
parser = argparse.ArgumentParser(
1825
description='Postprocess binaries to prepare for \
1926
their execution on Darwin platforms')
20-
parser.add_argument('bins', nargs='+', help='one or more binary files')
27+
parser.add_argument('bins', type=file_path, nargs='+', help='one or more binary files')
2128

2229
args = parser.parse_args(arguments)
2330

@@ -37,12 +44,12 @@ def unrpathize(filename):
3744
# `dyldinfo` has been replaced with `dyld_info`, so we try it first
3845
# before falling back to `dyldinfo`
3946
dylibsOutput = subprocess.check_output(
40-
['xcrun', 'dyld_info', '-dependents', filename],
47+
['/usr/bin/xcrun', 'dyld_info', '-dependents', filename],
4148
universal_newlines=True)
4249
except subprocess.CalledProcessError:
4350
sys.stderr.write("falling back to 'xcrun dyldinfo' ...\n")
4451
dylibsOutput = subprocess.check_output(
45-
['xcrun', 'dyldinfo', '-dylibs', filename],
52+
['/usr/bin/xcrun', 'dyldinfo', '-dylibs', filename],
4653
universal_newlines=True)
4754

4855
# Do not rewrite @rpath-relative load commands for these libraries:
@@ -71,7 +78,7 @@ def unrpathize(filename):
7178
r"(^|.*\s)(?P<path>@rpath/(?P<filename>libswift.*\.dylib))\s*$")
7279

7380
# Build a command to invoke install_name_tool.
74-
command = ['install_name_tool']
81+
command = ['/usr/bin/xcrun', 'install_name_tool']
7582
for line in dylibsOutput.splitlines():
7683
match = dylib_regex.match(line)
7784
if match and match.group('filename') not in allow_list:
@@ -82,7 +89,7 @@ def unrpathize(filename):
8289

8390
# Don't run the command if we didn't find any dylibs to change:
8491
# it's invalid to invoke install_name_tool without any operations.
85-
if len(command) == 1:
92+
if len(command) == 2:
8693
return
8794

8895
# The last argument is the filename to operate on.

utils/swift-rpathize.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,23 @@
3939
# /usr/lib/swift and changes them to use @rpath.
4040

4141
import argparse
42+
import os
4243
import re
4344
import subprocess
4445
import sys
4546

4647

48+
def file_path(string):
49+
if os.path.isfile(string):
50+
return string
51+
else:
52+
raise argparse.ArgumentTypeError(f"{string} is not a valid path")
53+
54+
4755
def main(arguments):
4856
parser = argparse.ArgumentParser(
4957
description='Change absolute install names to use @rpath')
50-
parser.add_argument('bin', help='the binary')
58+
parser.add_argument('bin', type=file_path, help='the binary')
5159

5260
args = parser.parse_args(arguments)
5361
rpathize(args.bin)
@@ -60,12 +68,12 @@ def rpathize(filename):
6068
# `dyldinfo` has been replaced with `dyld_info`, so we try it first
6169
# before falling back to `dyldinfo`
6270
dylibsOutput = subprocess.check_output(
63-
['xcrun', 'dyld_info', '-dependents', filename],
71+
['/usr/bin/xcrun', 'dyld_info', '-dependents', filename],
6472
universal_newlines=True)
6573
except subprocess.CalledProcessError:
6674
sys.stderr.write("falling back to 'xcrun dyldinfo' ...\n")
6775
dylibsOutput = subprocess.check_output(
68-
['xcrun', 'dyldinfo', '-dylibs', filename],
76+
['/usr/bin/xcrun', 'dyldinfo', '-dylibs', filename],
6977
universal_newlines=True)
7078

7179
# The output from dyldinfo -dylibs is a line of header followed by one
@@ -74,7 +82,7 @@ def rpathize(filename):
7482
r"(^|.*\s)(?P<path>/usr/lib/swift/(?P<filename>.*\.dylib))\s*$")
7583

7684
# Build a command to invoke install_name_tool.
77-
command = ['install_name_tool']
85+
command = ['/usr/bin/xcrun', 'install_name_tool']
7886
for line in dylibsOutput.splitlines():
7987
match = dylib_regex.match(line)
8088
if match:
@@ -85,7 +93,7 @@ def rpathize(filename):
8593

8694
# Don't run the command if we didn't find any dylibs to change:
8795
# it's invalid to invoke install_name_tool without any operations.
88-
if len(command) == 1:
96+
if len(command) == 2:
8997
return
9098

9199
# The last argument is the filename to operate on.

0 commit comments

Comments
 (0)