Path: blob/master/tools/testing/selftests/exec/binfmt_script.py
26285 views
#!/usr/bin/env python31# SPDX-License-Identifier: GPL-2.02#3# Test that truncation of bprm->buf doesn't cause unexpected execs paths, along4# with various other pathological cases.5import os, subprocess67# Relevant commits8#9# b5372fe5dc84 ("exec: load_script: Do not exec truncated interpreter path")10# 6eb3c3d0a52d ("exec: increase BINPRM_BUF_SIZE to 256")1112# BINPRM_BUF_SIZE13SIZE=2561415NAME_MAX=int(subprocess.check_output(["getconf", "NAME_MAX", "."]))1617test_num=018pass_num=019fail_num=02021code='''#!/usr/bin/perl22print "Executed interpreter! Args:\n";23print "0 : '$0'\n";24$counter = 1;25foreach my $a (@ARGV) {26print "$counter : '$a'\n";27$counter++;28}29'''3031##32# test - produce a binfmt_script hashbang line for testing33#34# @size: bytes for bprm->buf line, including hashbang but not newline35# @good: whether this script is expected to execute correctly36# @hashbang: the special 2 bytes for running binfmt_script37# @leading: any leading whitespace before the executable path38# @root: start of executable pathname39# @target: end of executable pathname40# @arg: bytes following the executable pathname41# @fill: character to fill between @root and @target to reach @size bytes42# @newline: character to use as newline, not counted towards @size43# ...44def test(name, size, good=True, leading="", root="./", target="/perl",45fill="A", arg="", newline="\n", hashbang="#!"):46global test_num, pass_num, fail_num, tests, NAME_MAX47test_num += 148if test_num > tests:49raise ValueError("more binfmt_script tests than expected! (want %d, expected %d)"50% (test_num, tests))5152middle = ""53remaining = size - len(hashbang) - len(leading) - len(root) - len(target) - len(arg)54# The middle of the pathname must not exceed NAME_MAX55while remaining >= NAME_MAX:56middle += fill * (NAME_MAX - 1)57middle += '/'58remaining -= NAME_MAX59middle += fill * remaining6061dirpath = root + middle62binary = dirpath + target63if len(target):64os.makedirs(dirpath, mode=0o755, exist_ok=True)65open(binary, "w").write(code)66os.chmod(binary, 0o755)6768buf=hashbang + leading + root + middle + target + arg + newline69if len(newline) > 0:70buf += 'echo this is not really perl\n'7172script = "binfmt_script-%s" % (name)73open(script, "w").write(buf)74os.chmod(script, 0o755)7576proc = subprocess.Popen(["./%s" % (script)], shell=True,77stdout=subprocess.PIPE, stderr=subprocess.STDOUT)78stdout = proc.communicate()[0]7980if proc.returncode == 0 and b'Executed interpreter' in stdout:81if good:82print("ok %d - binfmt_script %s (successful good exec)"83% (test_num, name))84pass_num += 185else:86print("not ok %d - binfmt_script %s succeeded when it should have failed"87% (test_num, name))88fail_num = 189else:90if good:91print("not ok %d - binfmt_script %s failed when it should have succeeded (rc:%d)"92% (test_num, name, proc.returncode))93fail_num = 194else:95print("ok %d - binfmt_script %s (correctly failed bad exec)"96% (test_num, name))97pass_num += 19899# Clean up crazy binaries100os.unlink(script)101if len(target):102elements = binary.split('/')103os.unlink(binary)104elements.pop()105while len(elements) > 1:106os.rmdir("/".join(elements))107elements.pop()108109tests=27110print("TAP version 1.3")111print("1..%d" % (tests))112113### FAIL (8 tests)114115# Entire path is well past the BINFMT_BUF_SIZE.116test(name="too-big", size=SIZE+80, good=False)117# Path is right at max size, making it impossible to tell if it was truncated.118test(name="exact", size=SIZE, good=False)119# Same as above, but with leading whitespace.120test(name="exact-space", size=SIZE, good=False, leading=" ")121# Huge buffer of only whitespace.122test(name="whitespace-too-big", size=SIZE+71, good=False, root="",123fill=" ", target="")124# A good path, but it gets truncated due to leading whitespace.125test(name="truncated", size=SIZE+17, good=False, leading=" " * 19)126# Entirely empty except for #!127test(name="empty", size=2, good=False, root="",128fill="", target="", newline="")129# Within size, but entirely spaces130test(name="spaces", size=SIZE-1, good=False, root="", fill=" ",131target="", newline="")132# Newline before binary.133test(name="newline-prefix", size=SIZE-1, good=False, leading="\n",134root="", fill=" ", target="")135136### ok (19 tests)137138# The original test case that was broken by commit:139# 8099b047ecc4 ("exec: load_script: don't blindly truncate shebang string")140test(name="test.pl", size=439, leading=" ",141root="./nix/store/bwav8kz8b3y471wjsybgzw84mrh4js9-perl-5.28.1/bin",142arg=" -I/nix/store/x6yyav38jgr924nkna62q3pkp0dgmzlx-perl5.28.1-File-Slurp-9999.25/lib/perl5/site_perl -I/nix/store/ha8v67sl8dac92r9z07vzr4gv1y9nwqz-perl5.28.1-Net-DBus-1.1.0/lib/perl5/site_perl -I/nix/store/dcrkvnjmwh69ljsvpbdjjdnqgwx90a9d-perl5.28.1-XML-Parser-2.44/lib/perl5/site_perl -I/nix/store/rmji88k2zz7h4zg97385bygcydrf2q8h-perl5.28.1-XML-Twig-3.52/lib/perl5/site_perl")143# One byte under size, leaving newline visible.144test(name="one-under", size=SIZE-1)145# Two bytes under size, leaving newline visible.146test(name="two-under", size=SIZE-2)147# Exact size, but trailing whitespace visible instead of newline148test(name="exact-trunc-whitespace", size=SIZE, arg=" ")149# Exact size, but trailing space and first arg char visible instead of newline.150test(name="exact-trunc-arg", size=SIZE, arg=" f")151# One bute under, with confirmed non-truncated arg since newline now visible.152test(name="one-under-full-arg", size=SIZE-1, arg=" f")153# Short read buffer by one byte.154test(name="one-under-no-nl", size=SIZE-1, newline="")155# Short read buffer by half buffer size.156test(name="half-under-no-nl", size=int(SIZE/2), newline="")157# One byte under with whitespace arg. leaving wenline visible.158test(name="one-under-trunc-arg", size=SIZE-1, arg=" ")159# One byte under with whitespace leading. leaving wenline visible.160test(name="one-under-leading", size=SIZE-1, leading=" ")161# One byte under with whitespace leading and as arg. leaving newline visible.162test(name="one-under-leading-trunc-arg", size=SIZE-1, leading=" ", arg=" ")163# Same as above, but with 2 bytes under164test(name="two-under-no-nl", size=SIZE-2, newline="")165test(name="two-under-trunc-arg", size=SIZE-2, arg=" ")166test(name="two-under-leading", size=SIZE-2, leading=" ")167test(name="two-under-leading-trunc-arg", size=SIZE-2, leading=" ", arg=" ")168# Same as above, but with buffer half filled169test(name="two-under-no-nl", size=int(SIZE/2), newline="")170test(name="two-under-trunc-arg", size=int(SIZE/2), arg=" ")171test(name="two-under-leading", size=int(SIZE/2), leading=" ")172test(name="two-under-lead-trunc-arg", size=int(SIZE/2), leading=" ", arg=" ")173174print("# Totals: pass:%d fail:%d xfail:0 xpass:0 skip:0 error:0" % (pass_num, fail_num))175176if test_num != tests:177raise ValueError("fewer binfmt_script tests than expected! (ran %d, expected %d"178% (test_num, tests))179180181