summaryrefslogtreecommitdiffstats
path: root/openstack/common/rootwrap/filters.py
diff options
context:
space:
mode:
authorRoman Podolyaka <rpodolyaka@mirantis.com>2013-03-29 23:05:52 +0200
committerRoman Podolyaka <rpodolyaka@mirantis.com>2013-04-01 17:45:17 +0300
commitae0b2762e2f467d4d3389859a602650384a2c14e (patch)
tree1cad823e07e3146ad97dd69321c6e8b775a6bbd2 /openstack/common/rootwrap/filters.py
parent05219b89b367b077a1e1f61a2767e71f7f44665a (diff)
downloadoslo-ae0b2762e2f467d4d3389859a602650384a2c14e.tar.gz
oslo-ae0b2762e2f467d4d3389859a602650384a2c14e.tar.xz
oslo-ae0b2762e2f467d4d3389859a602650384a2c14e.zip
Add PathFilter to rootwrap.
PathFilter is a type of filter that allows to check if path arguments of a command resolve to file system paths within given directories. Fixes bug 1098568. Change-Id: Ie2686ad2ff114075c6d8d804031b6e3fa60a43ca
Diffstat (limited to 'openstack/common/rootwrap/filters.py')
-rw-r--r--openstack/common/rootwrap/filters.py46
1 files changed, 46 insertions, 0 deletions
diff --git a/openstack/common/rootwrap/filters.py b/openstack/common/rootwrap/filters.py
index eadda25..d9618af 100644
--- a/openstack/common/rootwrap/filters.py
+++ b/openstack/common/rootwrap/filters.py
@@ -88,6 +88,52 @@ class RegExpFilter(CommandFilter):
return False
+class PathFilter(CommandFilter):
+ """Command filter checking that path arguments are within given dirs
+
+ One can specify the following constraints for command arguments:
+ 1) pass - pass an argument as is to the resulting command
+ 2) some_str - check if an argument is equal to the given string
+ 3) abs path - check if a path argument is within the given base dir
+
+ A typical rootwrapper filter entry looks like this:
+ # cmdname: filter name, raw command, user, arg_i_constraint [, ...]
+ chown: PathFilter, /bin/chown, root, nova, /var/lib/images
+
+ """
+
+ def match(self, userargs):
+ command, arguments = userargs[0], userargs[1:]
+
+ equal_args_num = len(self.args) == len(arguments)
+ exec_is_valid = super(PathFilter, self).match(userargs)
+ args_equal_or_pass = all(
+ arg == 'pass' or arg == value
+ for arg, value in zip(self.args, arguments)
+ if not os.path.isabs(arg) # arguments not specifying abs paths
+ )
+ paths_are_within_base_dirs = all(
+ os.path.commonprefix([arg, os.path.realpath(value)]) == arg
+ for arg, value in zip(self.args, arguments)
+ if os.path.isabs(arg) # arguments specifying abs paths
+ )
+
+ return (equal_args_num and
+ exec_is_valid and
+ args_equal_or_pass and
+ paths_are_within_base_dirs)
+
+ def get_command(self, userargs, exec_dirs=[]):
+ command, arguments = userargs[0], userargs[1:]
+
+ # convert path values to canonical ones; copy other args as is
+ args = [os.path.realpath(value) if os.path.isabs(arg) else value
+ for arg, value in zip(self.args, arguments)]
+
+ return super(PathFilter, self).get_command([command] + args,
+ exec_dirs)
+
+
class DnsmasqFilter(CommandFilter):
"""Specific filter for the dnsmasq call (which includes env)"""