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-24 11:33:13 +0100 |
commit | 3037db60f79a4b7e34bef14663ffa2523e9f0c03 (patch) | |
tree | 30edca3f34b4dc1f53b4cca2055f4eff8f09f35a /bitbake/lib/bb/utils.py | |
parent | 46f73593c03532b7293fde3147231422ac991081 (diff) | |
download | poky-3037db60f79a4b7e34bef14663ffa2523e9f0c03.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 master rev: 56cddeb9e1e4d249f84ccd6ef65db245636e38ea)
(Bitbake rev: fbf1c39641f78d553961974a2bb96256eb9496e7)
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 670e592fe0..2562db8e47 100644 --- a/bitbake/lib/bb/utils.py +++ b/bitbake/lib/bb/utils.py | |||
@@ -575,11 +575,30 @@ def build_environment(d): | |||
575 | if export: | 575 | if export: |
576 | os.environ[var] = d.getVar(var, True) or "" | 576 | os.environ[var] = d.getVar(var, True) or "" |
577 | 577 | ||
578 | def _check_unsafe_delete_path(path): | ||
579 | """ | ||
580 | Basic safeguard against recursively deleting something we shouldn't. If it returns True, | ||
581 | the caller should raise an exception with an appropriate message. | ||
582 | NOTE: This is NOT meant to be a security mechanism - just a guard against silly mistakes | ||
583 | with potentially disastrous results. | ||
584 | """ | ||
585 | extra = '' | ||
586 | # HOME might not be /home/something, so in case we can get it, check against it | ||
587 | homedir = os.environ.get('HOME', '') | ||
588 | if homedir: | ||
589 | extra = '|%s' % homedir | ||
590 | if re.match('(/|//|/home|/home/[^/]*%s)$' % extra, os.path.abspath(path)): | ||
591 | return True | ||
592 | return False | ||
593 | |||
578 | def remove(path, recurse=False): | 594 | def remove(path, recurse=False): |
579 | """Equivalent to rm -f or rm -rf""" | 595 | """Equivalent to rm -f or rm -rf""" |
580 | if not path: | 596 | if not path: |
581 | return | 597 | return |
582 | if recurse: | 598 | if recurse: |
599 | for name in glob.glob(path): | ||
600 | if _check_unsafe_delete_path(path): | ||
601 | raise Exception('bb.utils.remove: called with dangerous path "%s" and recurse=True, refusing to delete!' % path) | ||
583 | # shutil.rmtree(name) would be ideal but its too slow | 602 | # shutil.rmtree(name) would be ideal but its too slow |
584 | subprocess.call(['rm', '-rf'] + glob.glob(path)) | 603 | subprocess.call(['rm', '-rf'] + glob.glob(path)) |
585 | return | 604 | return |
@@ -593,6 +612,8 @@ def remove(path, recurse=False): | |||
593 | def prunedir(topdir): | 612 | def prunedir(topdir): |
594 | # Delete everything reachable from the directory named in 'topdir'. | 613 | # Delete everything reachable from the directory named in 'topdir'. |
595 | # CAUTION: This is dangerous! | 614 | # CAUTION: This is dangerous! |
615 | if _check_unsafe_delete_path(topdir): | ||
616 | raise Exception('bb.utils.prunedir: called with dangerous path "%s", refusing to delete!' % topdir) | ||
596 | for root, dirs, files in os.walk(topdir, topdown = False): | 617 | for root, dirs, files in os.walk(topdir, topdown = False): |
597 | for name in files: | 618 | for name in files: |
598 | os.remove(os.path.join(root, name)) | 619 | os.remove(os.path.join(root, name)) |