1#!/usr/bin/python
2#
3# Copyright 2015 The Chromium Authors. All rights reserved.
4# Use of this source code is governed by a BSD-style license that can be
5# found in the LICENSE file.
6
7import logging, os, tempfile
8from autotest_lib.client.bin import test, utils
9from autotest_lib.client.common_lib import error
10
11class security_mprotect(test.test):
12    """
13    Verify mprotect of PROT_EXEC works on noexec mounts.
14    """
15    version = 1
16    executable = 'prot_exec'
17
18
19    def setup(self):
20        os.chdir(self.srcdir)
21        utils.make(self.executable)
22
23
24    def run_once(self):
25        with tempfile.NamedTemporaryFile(prefix='%s-' % (self.executable),
26                                         dir='/run', delete=True) as temp:
27            temp_file_name = temp.name
28
29        r = utils.run("%s/%s %s" % (self.srcdir, self.executable,
30                                    temp_file_name),
31                      stdout_tee=utils.TEE_TO_LOGS,
32                      stderr_tee=utils.TEE_TO_LOGS,
33                      ignore_status=True)
34        if r.exit_status != 0 or len(r.stderr) > 0:
35            raise error.TestFail(r.stderr)
36        if 'skipping' in r.stdout:
37            logging.debug(r.stdout)
38            return
39        if 'pass' not in r.stdout:
40            raise error.TestFail(r.stdout)
41