• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import os
6
7from autotest_lib.client.bin import test, utils
8from autotest_lib.client.common_lib import error
9from autotest_lib.client.cros import cryptohome
10
11class platform_CryptohomeBadPerms(test.test):
12    """Tests Cryptohome's ability to detect directories with bad permissions or
13       ownership in the mount path of a home directory.
14    """
15    version = 1
16    cryptohome_proxy = None
17
18    def require_mount_fail(self, user):
19        """
20        Raise an error if the mound succeeded.
21        @param user: A random user created in run_once.
22        """
23        if self.cryptohome_proxy.mount(user, 'test', create=True):
24            raise error.TestFail('Mount unexpectedly succeeded for %s' % user)
25
26    def run_once(self):
27        self.cryptohome_proxy = cryptohome.CryptohomeProxy()
28
29        # Leaf element of user path not owned by user.
30        user = utils.random_username()
31        path = cryptohome.user_path(user)
32        os.mkdir(path)
33        os.chown(path, 0, 0)
34        try:
35            self.require_mount_fail(user)
36        finally:
37            os.rmdir(path)
38
39        # Leaf element of system path not owned by root.
40        user = utils.random_username()
41        path = cryptohome.system_path(user)
42        os.mkdir(path)
43        os.chown(path, 1, 1)
44        self.require_mount_fail(user)
45        try:
46            self.require_mount_fail(user)
47        finally:
48            os.rmdir(path)
49
50        # Leaf element of path too permissive.
51        user = utils.random_username()
52        path = cryptohome.user_path(user)
53        os.mkdir(path)
54        os.chmod(path, 0777)
55        self.require_mount_fail(user)
56        try:
57            self.require_mount_fail(user)
58        finally:
59            os.rmdir(path)
60
61        # Non-leaf element of path not owned by root.
62        user = utils.random_username()
63        path = cryptohome.user_path(user)
64        parent_path = os.path.dirname(path)
65        os.chown(parent_path, 1, 1)
66        try:
67            self.require_mount_fail(user)
68        finally:
69            os.chown(parent_path, 0, 0)
70
71        # Non-leaf element of path too permissive.
72        user = utils.random_username()
73        path = cryptohome.user_path(user)
74        parent_path = os.path.dirname(path)
75        old_perm = os.stat(parent_path).st_mode & 0777
76        os.chmod(parent_path, 0777)
77        try:
78            self.require_mount_fail(user)
79        finally:
80            os.chmod(parent_path, old_perm)
81            os.chown(parent_path, 0, 0)
82