diff options
| author | Paul Eggleton <paul.eggleton@linux.intel.com> | 2015-04-17 15:26:59 +0100 |
|---|---|---|
| committer | Richard Purdie <richard.purdie@linuxfoundation.org> | 2015-04-19 13:37:31 +0100 |
| commit | 72d88f29da1ece634028420317233a45ff8e015b (patch) | |
| tree | 78cbedae538c7895ca33c7f7ba734a05d5bedabf /bitbake/lib/bb/utils.py | |
| parent | f9b160571f795dc9d9d42ad7e85969ec54b86430 (diff) | |
| download | poky-72d88f29da1ece634028420317233a45ff8e015b.tar.gz | |
bitbake: lib/bb/utils: add safeguard against recursively deleting things we shouldn't
Add some very basic safeguard against recursively deleting paths such
as / and /home in the event of bugs or user mistakes.
Addresses [YOCTO #7620].
(Bitbake rev: 56cddeb9e1e4d249f84ccd6ef65db245636e38ea)
Signed-off-by: Paul Eggleton <paul.eggleton@linux.intel.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Diffstat (limited to 'bitbake/lib/bb/utils.py')
| -rw-r--r-- | bitbake/lib/bb/utils.py | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/bitbake/lib/bb/utils.py b/bitbake/lib/bb/utils.py index 5ac9bcfbd4..c97f3ef81f 100644 --- a/bitbake/lib/bb/utils.py +++ b/bitbake/lib/bb/utils.py | |||
| @@ -601,11 +601,30 @@ def build_environment(d): | |||
| 601 | if export: | 601 | if export: |
| 602 | os.environ[var] = d.getVar(var, True) or "" | 602 | os.environ[var] = d.getVar(var, True) or "" |
| 603 | 603 | ||
| 604 | def _check_unsafe_delete_path(path): | ||
| 605 | """ | ||
| 606 | Basic safeguard against recursively deleting something we shouldn't. If it returns True, | ||
| 607 | the caller should raise an exception with an appropriate message. | ||
| 608 | NOTE: This is NOT meant to be a security mechanism - just a guard against silly mistakes | ||
| 609 | with potentially disastrous results. | ||
| 610 | """ | ||
| 611 | extra = '' | ||
| 612 | # HOME might not be /home/something, so in case we can get it, check against it | ||
| 613 | homedir = os.environ.get('HOME', '') | ||
| 614 | if homedir: | ||
| 615 | extra = '|%s' % homedir | ||
| 616 | if re.match('(/|//|/home|/home/[^/]*%s)$' % extra, os.path.abspath(path)): | ||
| 617 | return True | ||
| 618 | return False | ||
| 619 | |||
| 604 | def remove(path, recurse=False): | 620 | def remove(path, recurse=False): |
| 605 | """Equivalent to rm -f or rm -rf""" | 621 | """Equivalent to rm -f or rm -rf""" |
| 606 | if not path: | 622 | if not path: |
| 607 | return | 623 | return |
| 608 | if recurse: | 624 | if recurse: |
| 625 | for name in glob.glob(path): | ||
| 626 | if _check_unsafe_delete_path(path): | ||
| 627 | raise Exception('bb.utils.remove: called with dangerous path "%s" and recurse=True, refusing to delete!' % path) | ||
| 609 | # shutil.rmtree(name) would be ideal but its too slow | 628 | # shutil.rmtree(name) would be ideal but its too slow |
| 610 | subprocess.call(['rm', '-rf'] + glob.glob(path)) | 629 | subprocess.call(['rm', '-rf'] + glob.glob(path)) |
| 611 | return | 630 | return |
| @@ -619,6 +638,8 @@ def remove(path, recurse=False): | |||
| 619 | def prunedir(topdir): | 638 | def prunedir(topdir): |
| 620 | # Delete everything reachable from the directory named in 'topdir'. | 639 | # Delete everything reachable from the directory named in 'topdir'. |
| 621 | # CAUTION: This is dangerous! | 640 | # CAUTION: This is dangerous! |
| 641 | if _check_unsafe_delete_path(topdir): | ||
| 642 | raise Exception('bb.utils.prunedir: called with dangerous path "%s", refusing to delete!' % topdir) | ||
| 622 | for root, dirs, files in os.walk(topdir, topdown = False): | 643 | for root, dirs, files in os.walk(topdir, topdown = False): |
| 623 | for name in files: | 644 | for name in files: |
| 624 | os.remove(os.path.join(root, name)) | 645 | os.remove(os.path.join(root, name)) |
