Bug 1258619 - Properly sandbox functions inside a template draft
authorMike Hommey <mh+mozilla@glandium.org>
Tue, 22 Mar 2016 15:31:37 +0900
changeset 343295 c0f28f8581f7f3b8a8becf8b027a60bf1e730253
parent 343294 ac2a1bb0f11b26b66433ce6f343247a3384fd6f9
child 516734 400b5f600467d2ace33e10b2804822af813b9d0e
push id13579
push userbmo:mh+mozilla@glandium.org
push dateTue, 22 Mar 2016 07:21:16 +0000
bugs1258619
milestone48.0a1
Bug 1258619 - Properly sandbox functions inside a template The way functions are being sandboxed in moz.configure land is that their global namespace is being replaced with a limited and identifiable dict. And we avoid re-wrapping a function that already received this treatment. The problem is that template functions have their global namespace replaced, and any function that is defined within the template inherits that global namespace. So when it comes time to wrap those functions defined in templates with e.g. depends, we detect that they're already wrapped although they are not, because we look if their global namespace is of the recognizable type we use when replacing it. So instead of looking at the global namespace type, keep track of all functions that are wrapped.
build/moz.configure/init.configure
python/mozbuild/mozbuild/configure/__init__.py
--- a/build/moz.configure/init.configure
+++ b/build/moz.configure/init.configure
@@ -234,19 +234,19 @@ def command_line_helper():
     # it is a one off and because the required functionality doesn't need
     # to be exposed for other usecases.
     return depends.__self__._helper
 
 
 # All options defined above this point can't be injected in mozconfig_options
 # below, so collect them.
 @template
-@advanced
 def early_options():
     @depends('--help')
+    @advanced
     def early_options(help):
         return set(
             option.env
             for option in depends.__self__._options.itervalues()
             if option.env
         )
     return early_options
 
--- a/python/mozbuild/mozbuild/configure/__init__.py
+++ b/python/mozbuild/mozbuild/configure/__init__.py
@@ -129,16 +129,19 @@ class ConfigureSandbox(dict):
         # - config set by each @depends function
         self._db = {}
 
         # Store options added with `imply_option`, and the reason they were
         # added (which can either have been given to `imply_option`, or
         # infered.
         self._implied_options = {}
 
+        # Store all results from _prepare_function
+        self.__prepared_functions = set()
+
         self._helper = CommandLineHelper(environ, argv)
 
         self._config, self._stdout, self._stderr = config, stdout, stderr
 
         self._help = None
         self._help_option = self.option_impl('--help',
                                              help='print this message')
         self._seen.add(self._help_option)
@@ -423,25 +426,26 @@ class ConfigureSandbox(dict):
         return func
 
     def _prepare_function(self, func):
         '''Alter the given function global namespace with the common ground
         for @depends, @template and @advanced.
         '''
         if not inspect.isfunction(func):
             raise TypeError("Unexpected type: '%s'" % type(func))
-        if isinstance(func.func_globals, SandboxedGlobal):
+        if func in self.__prepared_functions:
             return func, func.func_globals
 
         glob = SandboxedGlobal(func.func_globals)
         glob.update(
             __builtins__=self.BUILTINS,
             __file__=self._paths[-1],
             os=self.OS,
         )
         func = wraps(func)(types.FunctionType(
             func.func_code,
             glob,
             func.__name__,
             func.func_defaults,
             func.func_closure
         ))
+        self.__prepared_functions.add(func)
         return func, glob