summaryrefslogtreecommitdiffstats
path: root/bitbake/lib/bb/utils.py
diff options
context:
space:
mode:
Diffstat (limited to 'bitbake/lib/bb/utils.py')
-rw-r--r--bitbake/lib/bb/utils.py21
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
604def _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
604def remove(path, recurse=False): 620def 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):
619def prunedir(topdir): 638def 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))