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