Frank Farzan | d5e3631 | 2012-01-13 14:34:03 -0800 | [diff] [blame] | 1 | # Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Chris Masone | 5e06f18 | 2010-03-23 08:29:52 -0700 | [diff] [blame] | 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
Eric Caruso | 8dc4098 | 2018-03-20 17:05:19 -0700 | [diff] [blame] | 5 | import dbus, gobject, logging, os, random, re, shutil, string, sys, time |
Will Drewry | 9e44079 | 2013-12-11 17:18:35 -0600 | [diff] [blame] | 6 | from dbus.mainloop.glib import DBusGMainLoop |
barfab@chromium.org | b6d2993 | 2012-04-11 09:46:43 +0200 | [diff] [blame] | 7 | |
Hsinyu Chao | e0b08e6 | 2015-08-11 10:50:37 +0000 | [diff] [blame] | 8 | import common, constants |
barfab@chromium.org | 5c37463 | 2012-04-05 16:50:56 +0200 | [diff] [blame] | 9 | from autotest_lib.client.bin import utils |
Chris Masone | 5e06f18 | 2010-03-23 08:29:52 -0700 | [diff] [blame] | 10 | from autotest_lib.client.common_lib import error |
Will Drewry | 9e44079 | 2013-12-11 17:18:35 -0600 | [diff] [blame] | 11 | from autotest_lib.client.cros.cros_disks import DBusClient |
Eric Li | c4d8f4a | 2010-12-10 09:49:23 -0800 | [diff] [blame] | 12 | |
Sean O | e5d8fd0 | 2010-09-30 10:44:44 +0200 | [diff] [blame] | 13 | CRYPTOHOME_CMD = '/usr/sbin/cryptohome' |
Elly Fong-Jones | 6cb26ad | 2013-05-21 12:09:23 -0400 | [diff] [blame] | 14 | GUEST_USER_NAME = '$guest' |
Kris Rambish | bb5258c | 2014-12-16 16:51:17 -0800 | [diff] [blame] | 15 | UNAVAILABLE_ACTION = 'Unknown action or no action given.' |
Alexis Savery | ccb16be | 2017-02-01 16:23:15 -0800 | [diff] [blame] | 16 | MOUNT_RETRY_COUNT = 20 |
Dan Spaid | be9789c | 2017-05-19 15:18:42 +0900 | [diff] [blame] | 17 | TEMP_MOUNT_PATTERN = '/home/.shadow/%s/temporary_mount' |
| 18 | VAULT_PATH_PATTERN = '/home/.shadow/%s/vault' |
Sean O | e5d8fd0 | 2010-09-30 10:44:44 +0200 | [diff] [blame] | 19 | |
Eric Caruso | 8dc4098 | 2018-03-20 17:05:19 -0700 | [diff] [blame] | 20 | DBUS_PROTOS_DEP = 'dbus_protos' |
| 21 | |
| 22 | |
Chris Masone | 5d010aa | 2013-05-06 14:38:42 -0700 | [diff] [blame] | 23 | class ChromiumOSError(error.TestError): |
Sean O | e5d8fd0 | 2010-09-30 10:44:44 +0200 | [diff] [blame] | 24 | """Generic error for ChromiumOS-specific exceptions.""" |
| 25 | pass |
| 26 | |
Sean O | e5d8fd0 | 2010-09-30 10:44:44 +0200 | [diff] [blame] | 27 | def __run_cmd(cmd): |
| 28 | return utils.system_output(cmd + ' 2>&1', retain_output=True, |
| 29 | ignore_status=True).strip() |
| 30 | |
Sean O | e5d8fd0 | 2010-09-30 10:44:44 +0200 | [diff] [blame] | 31 | def get_user_hash(user): |
barfab@chromium.org | 5c37463 | 2012-04-05 16:50:56 +0200 | [diff] [blame] | 32 | """Get the user hash for the given user.""" |
Elly Fong-Jones | 6cb26ad | 2013-05-21 12:09:23 -0400 | [diff] [blame] | 33 | return utils.system_output(['cryptohome', '--action=obfuscate_user', |
| 34 | '--user=%s' % user]) |
Sean O | e5d8fd0 | 2010-09-30 10:44:44 +0200 | [diff] [blame] | 35 | |
| 36 | |
barfab@chromium.org | 5c37463 | 2012-04-05 16:50:56 +0200 | [diff] [blame] | 37 | def user_path(user): |
| 38 | """Get the user mount point for the given user.""" |
Elly Fong-Jones | 6cb26ad | 2013-05-21 12:09:23 -0400 | [diff] [blame] | 39 | return utils.system_output(['cryptohome-path', 'user', user]) |
barfab@chromium.org | 5c37463 | 2012-04-05 16:50:56 +0200 | [diff] [blame] | 40 | |
| 41 | |
| 42 | def system_path(user): |
| 43 | """Get the system mount point for the given user.""" |
Elly Fong-Jones | 6cb26ad | 2013-05-21 12:09:23 -0400 | [diff] [blame] | 44 | return utils.system_output(['cryptohome-path', 'system', user]) |
barfab@chromium.org | 5c37463 | 2012-04-05 16:50:56 +0200 | [diff] [blame] | 45 | |
| 46 | |
Dan Spaid | be9789c | 2017-05-19 15:18:42 +0900 | [diff] [blame] | 47 | def temporary_mount_path(user): |
| 48 | """Get the vault mount path used during crypto-migration for the user. |
| 49 | |
| 50 | @param user: user the temporary mount should be for |
| 51 | """ |
| 52 | return TEMP_MOUNT_PATTERN % (get_user_hash(user)) |
| 53 | |
| 54 | |
| 55 | def vault_path(user): |
| 56 | """ Get the vault path for the given user. |
| 57 | |
| 58 | @param user: The user who's vault path should be returned. |
| 59 | """ |
| 60 | return VAULT_PATH_PATTERN % (get_user_hash(user)) |
| 61 | |
| 62 | |
Chris Masone | 5d010aa | 2013-05-06 14:38:42 -0700 | [diff] [blame] | 63 | def ensure_clean_cryptohome_for(user, password=None): |
| 64 | """Ensure a fresh cryptohome exists for user. |
| 65 | |
| 66 | @param user: user who needs a shiny new cryptohome. |
| 67 | @param password: if unset, a random password will be used. |
| 68 | """ |
| 69 | if not password: |
| 70 | password = ''.join(random.sample(string.ascii_lowercase, 6)) |
Eric Caruso | 272193b | 2018-02-05 14:02:11 -0800 | [diff] [blame] | 71 | unmount_vault(user) |
Chris Masone | 5d010aa | 2013-05-06 14:38:42 -0700 | [diff] [blame] | 72 | remove_vault(user) |
| 73 | mount_vault(user, password, create=True) |
| 74 | |
| 75 | |
Frank Farzan | d5e3631 | 2012-01-13 14:34:03 -0800 | [diff] [blame] | 76 | def get_tpm_status(): |
| 77 | """Get the TPM status. |
| 78 | |
| 79 | Returns: |
| 80 | A TPM status dictionary, for example: |
| 81 | { 'Enabled': True, |
| 82 | 'Owned': True, |
| 83 | 'Being Owned': False, |
| 84 | 'Ready': True, |
| 85 | 'Password': '' |
| 86 | } |
| 87 | """ |
| 88 | out = __run_cmd(CRYPTOHOME_CMD + ' --action=tpm_status') |
| 89 | status = {} |
| 90 | for field in ['Enabled', 'Owned', 'Being Owned', 'Ready']: |
| 91 | match = re.search('TPM %s: (true|false)' % field, out) |
| 92 | if not match: |
| 93 | raise ChromiumOSError('Invalid TPM status: "%s".' % out) |
| 94 | status[field] = match.group(1) == 'true' |
| 95 | match = re.search('TPM Password: (\w*)', out) |
| 96 | status['Password'] = '' |
| 97 | if match: |
| 98 | status['Password'] = match.group(1) |
| 99 | return status |
| 100 | |
| 101 | |
Kris Rambish | 82ee1c0 | 2014-12-10 17:02:39 -0800 | [diff] [blame] | 102 | def get_tpm_more_status(): |
| 103 | """Get more of the TPM status. |
| 104 | |
| 105 | Returns: |
| 106 | A TPM more status dictionary, for example: |
| 107 | { 'dictionary_attack_lockout_in_effect': False, |
| 108 | 'attestation_prepared': False, |
| 109 | 'boot_lockbox_finalized': False, |
| 110 | 'enabled': True, |
| 111 | 'owned': True, |
Kris Rambish | be13259 | 2014-12-17 14:26:06 -0800 | [diff] [blame] | 112 | 'owner_password': '' |
Kris Rambish | 82ee1c0 | 2014-12-10 17:02:39 -0800 | [diff] [blame] | 113 | 'dictionary_attack_counter': 0, |
| 114 | 'dictionary_attack_lockout_seconds_remaining': 0, |
| 115 | 'dictionary_attack_threshold': 10, |
| 116 | 'attestation_enrolled': False, |
| 117 | 'initialized': True, |
| 118 | 'verified_boot_measured': False, |
| 119 | 'install_lockbox_finalized': True |
| 120 | } |
Kris Rambish | bb5258c | 2014-12-16 16:51:17 -0800 | [diff] [blame] | 121 | An empty dictionary is returned if the command is not supported. |
Kris Rambish | 82ee1c0 | 2014-12-10 17:02:39 -0800 | [diff] [blame] | 122 | """ |
Kris Rambish | 82ee1c0 | 2014-12-10 17:02:39 -0800 | [diff] [blame] | 123 | status = {} |
Kris Rambish | bb5258c | 2014-12-16 16:51:17 -0800 | [diff] [blame] | 124 | out = __run_cmd(CRYPTOHOME_CMD + ' --action=tpm_more_status | grep :') |
| 125 | if out.startswith(UNAVAILABLE_ACTION): |
| 126 | # --action=tpm_more_status only exists >= 41. |
| 127 | logging.info('Method not supported!') |
| 128 | return status |
Kris Rambish | 82ee1c0 | 2014-12-10 17:02:39 -0800 | [diff] [blame] | 129 | for line in out.splitlines(): |
| 130 | items = line.strip().split(':') |
| 131 | if items[1].strip() == 'false': |
| 132 | value = False |
| 133 | elif items[1].strip() == 'true': |
| 134 | value = True |
Kris Rambish | be13259 | 2014-12-17 14:26:06 -0800 | [diff] [blame] | 135 | elif items[1].strip().isdigit(): |
Kris Rambish | 82ee1c0 | 2014-12-10 17:02:39 -0800 | [diff] [blame] | 136 | value = int(items[1].strip()) |
Kris Rambish | be13259 | 2014-12-17 14:26:06 -0800 | [diff] [blame] | 137 | else: |
| 138 | value = items[1].strip(' "') |
Kris Rambish | 82ee1c0 | 2014-12-10 17:02:39 -0800 | [diff] [blame] | 139 | status[items[0]] = value |
| 140 | return status |
| 141 | |
| 142 | |
Mary Ruthven | 9a0ce56 | 2017-05-30 13:01:47 -0700 | [diff] [blame] | 143 | def get_fwmp(cleared_fwmp=False): |
| 144 | """Get the firmware management parameters. |
| 145 | |
| 146 | Args: |
| 147 | cleared_fwmp: True if the space should not exist. |
| 148 | |
| 149 | Returns: |
| 150 | The dictionary with the FWMP contents, for example: |
| 151 | { 'flags': 0xbb41, |
| 152 | 'developer_key_hash': |
| 153 | "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\ |
| 154 | 000\000\000\000\000\000\000\000\000\000\000", |
| 155 | } |
| 156 | or a dictionary with the Error if the FWMP doesn't exist and |
| 157 | cleared_fwmp is True |
| 158 | { 'error': 'CRYPTOHOME_ERROR_FIRMWARE_MANAGEMENT_PARAMETERS_INVALID' } |
| 159 | |
| 160 | Raises: |
| 161 | ChromiumOSError if any expected field is not found in the cryptohome |
| 162 | output. This would typically happen when FWMP state does not match |
| 163 | 'clreared_fwmp' |
| 164 | """ |
| 165 | out = __run_cmd(CRYPTOHOME_CMD + |
| 166 | ' --action=get_firmware_management_parameters') |
| 167 | |
| 168 | if cleared_fwmp: |
| 169 | fields = ['error'] |
| 170 | else: |
| 171 | fields = ['flags', 'developer_key_hash'] |
| 172 | |
| 173 | status = {} |
| 174 | for field in fields: |
| 175 | match = re.search('%s: (\S+)\n' % field, out) |
| 176 | if not match: |
| 177 | raise ChromiumOSError('Invalid FWMP field %s: "%s".' % |
| 178 | (field, out)) |
| 179 | status[field] = match.group(1) |
| 180 | return status |
| 181 | |
| 182 | |
| 183 | def set_fwmp(flags, developer_key_hash=None): |
| 184 | """Set the firmware management parameter contents. |
| 185 | |
| 186 | Args: |
| 187 | developer_key_hash: a string with the developer key hash |
| 188 | |
| 189 | Raises: |
| 190 | ChromiumOSError cryptohome cannot set the FWMP contents |
| 191 | """ |
| 192 | cmd = (CRYPTOHOME_CMD + |
| 193 | ' --action=set_firmware_management_parameters ' |
| 194 | '--flags=' + flags) |
| 195 | if developer_key_hash: |
| 196 | cmd += ' --developer_key_hash=' + developer_key_hash |
| 197 | |
| 198 | out = __run_cmd(cmd) |
| 199 | if 'SetFirmwareManagementParameters success' not in out: |
| 200 | raise ChromiumOSError('failed to set FWMP: %s' % out) |
| 201 | |
| 202 | |
Kris Rambish | 82ee1c0 | 2014-12-10 17:02:39 -0800 | [diff] [blame] | 203 | def is_tpm_lockout_in_effect(): |
| 204 | """Returns true if the TPM lockout is in effect; false otherwise.""" |
| 205 | status = get_tpm_more_status() |
Christopher Wiley | 94fd6b3 | 2014-12-13 18:52:03 -0800 | [diff] [blame] | 206 | return status.get('dictionary_attack_lockout_in_effect', None) |
Kris Rambish | 82ee1c0 | 2014-12-10 17:02:39 -0800 | [diff] [blame] | 207 | |
| 208 | |
David Pursell | 2a2ef34 | 2014-10-17 10:34:56 -0700 | [diff] [blame] | 209 | def get_login_status(): |
| 210 | """Query the login status |
| 211 | |
| 212 | Returns: |
| 213 | A login status dictionary containing: |
| 214 | { 'owner_user_exists': True|False, |
| 215 | 'boot_lockbox_finalized': True|False |
| 216 | } |
| 217 | """ |
| 218 | out = __run_cmd(CRYPTOHOME_CMD + ' --action=get_login_status') |
| 219 | status = {} |
| 220 | for field in ['owner_user_exists', 'boot_lockbox_finalized']: |
| 221 | match = re.search('%s: (true|false)' % field, out) |
| 222 | if not match: |
| 223 | raise ChromiumOSError('Invalid login status: "%s".' % out) |
| 224 | status[field] = match.group(1) == 'true' |
| 225 | return status |
| 226 | |
| 227 | |
Darren Krahn | 5f880f6 | 2012-10-02 15:17:59 -0700 | [diff] [blame] | 228 | def get_tpm_attestation_status(): |
| 229 | """Get the TPM attestation status. Works similar to get_tpm_status(). |
| 230 | """ |
| 231 | out = __run_cmd(CRYPTOHOME_CMD + ' --action=tpm_attestation_status') |
| 232 | status = {} |
| 233 | for field in ['Prepared', 'Enrolled']: |
| 234 | match = re.search('Attestation %s: (true|false)' % field, out) |
| 235 | if not match: |
| 236 | raise ChromiumOSError('Invalid attestation status: "%s".' % out) |
| 237 | status[field] = match.group(1) == 'true' |
| 238 | return status |
| 239 | |
| 240 | |
Eric Caruso | 6da07a0 | 2018-02-07 16:02:41 -0800 | [diff] [blame] | 241 | def take_tpm_ownership(wait_for_ownership=True): |
Frank Farzan | d5e3631 | 2012-01-13 14:34:03 -0800 | [diff] [blame] | 242 | """Take TPM owernship. |
| 243 | |
Eric Caruso | 6da07a0 | 2018-02-07 16:02:41 -0800 | [diff] [blame] | 244 | Args: |
| 245 | wait_for_ownership: block until TPM is owned if true |
Frank Farzan | d5e3631 | 2012-01-13 14:34:03 -0800 | [diff] [blame] | 246 | """ |
| 247 | __run_cmd(CRYPTOHOME_CMD + ' --action=tpm_take_ownership') |
Eric Caruso | 6da07a0 | 2018-02-07 16:02:41 -0800 | [diff] [blame] | 248 | if wait_for_ownership: |
Maksim Ivanov | 21c967a | 2018-03-22 21:24:41 +0100 | [diff] [blame] | 249 | # Note that waiting for the 'Ready' flag is more correct than waiting |
| 250 | # for the 'Owned' flag, as the latter is set by cryptohomed before some |
| 251 | # of the ownership tasks are completed. |
| 252 | utils.poll_for_condition( |
| 253 | lambda: get_tpm_status()['Ready'], |
| 254 | timeout=300, |
| 255 | exception=error.TestError('Timeout waiting for TPM ownership')) |
Frank Farzan | d5e3631 | 2012-01-13 14:34:03 -0800 | [diff] [blame] | 256 | |
| 257 | |
Darren Krahn | 0e73e7f | 2012-09-05 15:35:15 -0700 | [diff] [blame] | 258 | def verify_ek(): |
| 259 | """Verify the TPM endorsement key. |
| 260 | |
| 261 | Returns true if EK is valid. |
| 262 | """ |
| 263 | cmd = CRYPTOHOME_CMD + ' --action=tpm_verify_ek' |
| 264 | return (utils.system(cmd, ignore_status=True) == 0) |
| 265 | |
| 266 | |
Sean O | e5d8fd0 | 2010-09-30 10:44:44 +0200 | [diff] [blame] | 267 | def remove_vault(user): |
barfab@chromium.org | 5c37463 | 2012-04-05 16:50:56 +0200 | [diff] [blame] | 268 | """Remove the given user's vault from the shadow directory.""" |
Sean O | e5d8fd0 | 2010-09-30 10:44:44 +0200 | [diff] [blame] | 269 | logging.debug('user is %s', user) |
| 270 | user_hash = get_user_hash(user) |
Gwendal Grignou | 6bad672 | 2016-06-09 16:36:04 -0700 | [diff] [blame] | 271 | logging.debug('Removing vault for user %s with hash %s', user, user_hash) |
Sean O | e5d8fd0 | 2010-09-30 10:44:44 +0200 | [diff] [blame] | 272 | cmd = CRYPTOHOME_CMD + ' --action=remove --force --user=%s' % user |
| 273 | __run_cmd(cmd) |
barfab@chromium.org | 5c37463 | 2012-04-05 16:50:56 +0200 | [diff] [blame] | 274 | # Ensure that the vault does not exist. |
| 275 | if os.path.exists(os.path.join(constants.SHADOW_ROOT, user_hash)): |
Darren Krahn | e6c44b9 | 2014-03-31 12:11:08 -0700 | [diff] [blame] | 276 | raise ChromiumOSError('Cryptohome could not remove the user\'s vault.') |
Sean O | e5d8fd0 | 2010-09-30 10:44:44 +0200 | [diff] [blame] | 277 | |
| 278 | |
barfab@chromium.org | cf2151e | 2012-04-04 15:39:34 +0200 | [diff] [blame] | 279 | def remove_all_vaults(): |
| 280 | """Remove any existing vaults from the shadow directory. |
| 281 | |
| 282 | This function must be run with root privileges. |
| 283 | """ |
barfab@chromium.org | 5c37463 | 2012-04-05 16:50:56 +0200 | [diff] [blame] | 284 | for item in os.listdir(constants.SHADOW_ROOT): |
| 285 | abs_item = os.path.join(constants.SHADOW_ROOT, item) |
barfab@chromium.org | cf2151e | 2012-04-04 15:39:34 +0200 | [diff] [blame] | 286 | if os.path.isdir(os.path.join(abs_item, 'vault')): |
Gwendal Grignou | 6bad672 | 2016-06-09 16:36:04 -0700 | [diff] [blame] | 287 | logging.debug('Removing vault for user with hash %s', item) |
barfab@chromium.org | cf2151e | 2012-04-04 15:39:34 +0200 | [diff] [blame] | 288 | shutil.rmtree(abs_item) |
| 289 | |
| 290 | |
Eric Caruso | 22acd67 | 2018-02-01 17:55:28 -0800 | [diff] [blame] | 291 | def mount_vault(user, password, create=False, key_label='bar'): |
| 292 | """Mount the given user's vault. Mounts should be created by calling this |
| 293 | function with create=True, and can be used afterwards with create=False. |
| 294 | Only try to mount existing vaults created with this function. |
| 295 | |
| 296 | """ |
| 297 | args = [CRYPTOHOME_CMD, '--action=mount_ex', '--user=%s' % user, |
Chris Masone | 3543e51 | 2013-11-04 13:09:30 -0800 | [diff] [blame] | 298 | '--password=%s' % password, '--async'] |
Sean O | e5d8fd0 | 2010-09-30 10:44:44 +0200 | [diff] [blame] | 299 | if create: |
Eric Caruso | 22acd67 | 2018-02-01 17:55:28 -0800 | [diff] [blame] | 300 | args += ['--key_label=%s' % key_label, '--create'] |
Chris Masone | 3543e51 | 2013-11-04 13:09:30 -0800 | [diff] [blame] | 301 | logging.info(__run_cmd(' '.join(args))) |
barfab@chromium.org | 5c37463 | 2012-04-05 16:50:56 +0200 | [diff] [blame] | 302 | # Ensure that the vault exists in the shadow directory. |
Sean O | e5d8fd0 | 2010-09-30 10:44:44 +0200 | [diff] [blame] | 303 | user_hash = get_user_hash(user) |
barfab@chromium.org | 5c37463 | 2012-04-05 16:50:56 +0200 | [diff] [blame] | 304 | if not os.path.exists(os.path.join(constants.SHADOW_ROOT, user_hash)): |
Alexis Savery | ccb16be | 2017-02-01 16:23:15 -0800 | [diff] [blame] | 305 | retry = 0 |
| 306 | mounted = False |
| 307 | while retry < MOUNT_RETRY_COUNT and not mounted: |
| 308 | time.sleep(1) |
| 309 | logging.info("Retry " + str(retry + 1)) |
| 310 | __run_cmd(' '.join(args)) |
| 311 | # TODO: Remove this additional call to get_user_hash(user) when |
| 312 | # crbug.com/690994 is fixed |
| 313 | user_hash = get_user_hash(user) |
| 314 | if os.path.exists(os.path.join(constants.SHADOW_ROOT, user_hash)): |
| 315 | mounted = True |
| 316 | retry += 1 |
| 317 | if not mounted: |
| 318 | raise ChromiumOSError('Cryptohome vault not found after mount.') |
barfab@chromium.org | 5c37463 | 2012-04-05 16:50:56 +0200 | [diff] [blame] | 319 | # Ensure that the vault is mounted. |
Gwendal Grignou | 6bad672 | 2016-06-09 16:36:04 -0700 | [diff] [blame] | 320 | if not is_permanent_vault_mounted(user=user, allow_fail=True): |
barfab@chromium.org | 5c37463 | 2012-04-05 16:50:56 +0200 | [diff] [blame] | 321 | raise ChromiumOSError('Cryptohome created a vault but did not mount.') |
Sean O | e5d8fd0 | 2010-09-30 10:44:44 +0200 | [diff] [blame] | 322 | |
| 323 | |
Chris Masone | 5d010aa | 2013-05-06 14:38:42 -0700 | [diff] [blame] | 324 | def mount_guest(): |
Sergey Poromov | 533008f | 2017-10-13 14:07:43 +0200 | [diff] [blame] | 325 | """Mount the guest vault.""" |
Greg Kerr | d7cdc13 | 2018-06-08 11:55:40 -0700 | [diff] [blame] | 326 | args = [CRYPTOHOME_CMD, '--action=mount_guest_ex'] |
Chris Masone | 3543e51 | 2013-11-04 13:09:30 -0800 | [diff] [blame] | 327 | logging.info(__run_cmd(' '.join(args))) |
Sergey Poromov | 533008f | 2017-10-13 14:07:43 +0200 | [diff] [blame] | 328 | # Ensure that the guest vault is mounted. |
Chris Masone | 5d010aa | 2013-05-06 14:38:42 -0700 | [diff] [blame] | 329 | if not is_guest_vault_mounted(allow_fail=True): |
Sergey Poromov | 533008f | 2017-10-13 14:07:43 +0200 | [diff] [blame] | 330 | raise ChromiumOSError('Cryptohome did not mount guest vault.') |
Chris Masone | 5d010aa | 2013-05-06 14:38:42 -0700 | [diff] [blame] | 331 | |
| 332 | |
Sean O | e5d8fd0 | 2010-09-30 10:44:44 +0200 | [diff] [blame] | 333 | def test_auth(user, password): |
Eric Caruso | de07cf8 | 2018-02-12 15:34:02 -0800 | [diff] [blame] | 334 | cmd = [CRYPTOHOME_CMD, '--action=check_key_ex', '--user=%s' % user, |
Elly Fong-Jones | 6cb26ad | 2013-05-21 12:09:23 -0400 | [diff] [blame] | 335 | '--password=%s' % password, '--async'] |
Eric Caruso | de07cf8 | 2018-02-12 15:34:02 -0800 | [diff] [blame] | 336 | out = __run_cmd(' '.join(cmd)) |
| 337 | logging.info(out) |
| 338 | return 'Key authenticated.' in out |
Sean O | e5d8fd0 | 2010-09-30 10:44:44 +0200 | [diff] [blame] | 339 | |
| 340 | |
Elly Fong-Jones | 6cb26ad | 2013-05-21 12:09:23 -0400 | [diff] [blame] | 341 | def unmount_vault(user): |
barfab@chromium.org | 5c37463 | 2012-04-05 16:50:56 +0200 | [diff] [blame] | 342 | """Unmount the given user's vault. |
| 343 | |
| 344 | Once unmounting for a specific user is supported, the user parameter will |
| 345 | name the target user. See crosbug.com/20778. |
Elly Jones | 686c2f4 | 2011-10-24 16:45:07 -0400 | [diff] [blame] | 346 | """ |
Chris Masone | 3543e51 | 2013-11-04 13:09:30 -0800 | [diff] [blame] | 347 | __run_cmd(CRYPTOHOME_CMD + ' --action=unmount') |
barfab@chromium.org | 5c37463 | 2012-04-05 16:50:56 +0200 | [diff] [blame] | 348 | # Ensure that the vault is not mounted. |
Elly Fong-Jones | 6cb26ad | 2013-05-21 12:09:23 -0400 | [diff] [blame] | 349 | if is_vault_mounted(user, allow_fail=True): |
Sean O | e5d8fd0 | 2010-09-30 10:44:44 +0200 | [diff] [blame] | 350 | raise ChromiumOSError('Cryptohome did not unmount the user.') |
| 351 | |
| 352 | |
barfab@chromium.org | 5c37463 | 2012-04-05 16:50:56 +0200 | [diff] [blame] | 353 | def __get_mount_info(mount_point, allow_fail=False): |
| 354 | """Get information about the active mount at a given mount point.""" |
beeps | 569f867 | 2013-08-07 10:18:51 -0700 | [diff] [blame] | 355 | cryptohomed_path = '/proc/$(pgrep cryptohomed)/mounts' |
| 356 | try: |
Daniel Erat | 2ec3279 | 2017-01-31 18:26:59 -0700 | [diff] [blame] | 357 | logging.debug("Active cryptohome mounts:\n" + |
| 358 | utils.system_output('cat %s' % cryptohomed_path)) |
beeps | 569f867 | 2013-08-07 10:18:51 -0700 | [diff] [blame] | 359 | mount_line = utils.system_output( |
| 360 | 'grep %s %s' % (mount_point, cryptohomed_path), |
| 361 | ignore_status=allow_fail) |
| 362 | except Exception as e: |
| 363 | logging.error(e) |
| 364 | raise ChromiumOSError('Could not get info about cryptohome vault ' |
| 365 | 'through %s. See logs for complete mount-point.' |
| 366 | % os.path.dirname(str(mount_point))) |
Sourav Poddar | 574bd62 | 2010-05-26 14:22:26 +0530 | [diff] [blame] | 367 | return mount_line.split() |
| 368 | |
| 369 | |
Elly Fong-Jones | 6cb26ad | 2013-05-21 12:09:23 -0400 | [diff] [blame] | 370 | def __get_user_mount_info(user, allow_fail=False): |
barfab@chromium.org | 5c37463 | 2012-04-05 16:50:56 +0200 | [diff] [blame] | 371 | """Get information about the active mounts for a given user. |
| 372 | |
| 373 | Returns the active mounts at the user's user and system mount points. If no |
| 374 | user is given, the active mount at the shared mount point is returned |
| 375 | (regular users have a bind-mount at this mount point for backwards |
| 376 | compatibility; the guest user has a mount at this mount point only). |
| 377 | """ |
Elly Fong-Jones | 6cb26ad | 2013-05-21 12:09:23 -0400 | [diff] [blame] | 378 | return [__get_mount_info(mount_point=user_path(user), |
| 379 | allow_fail=allow_fail), |
| 380 | __get_mount_info(mount_point=system_path(user), |
| 381 | allow_fail=allow_fail)] |
Jim Hebert | f08f88d | 2011-04-22 10:33:49 -0700 | [diff] [blame] | 382 | |
Gwendal Grignou | 6bad672 | 2016-06-09 16:36:04 -0700 | [diff] [blame] | 383 | def is_vault_mounted(user, regexes=None, allow_fail=False): |
barfab@chromium.org | 5c37463 | 2012-04-05 16:50:56 +0200 | [diff] [blame] | 384 | """Check whether a vault is mounted for the given user. |
| 385 | |
Gwendal Grignou | 6bad672 | 2016-06-09 16:36:04 -0700 | [diff] [blame] | 386 | user: If no user is given, the shared mount point is checked, determining |
| 387 | whether a vault is mounted for any user. |
| 388 | regexes: dictionary of regexes to matches against the mount information. |
| 389 | The mount filesystem for the user's user and system mounts point must |
| 390 | match one of the keys. |
| 391 | The mount source point must match the selected device regex. |
| 392 | |
| 393 | In addition, if mounted over ext4, we check the directory is encrypted. |
barfab@chromium.org | 5c37463 | 2012-04-05 16:50:56 +0200 | [diff] [blame] | 394 | """ |
Gwendal Grignou | 6bad672 | 2016-06-09 16:36:04 -0700 | [diff] [blame] | 395 | if regexes is None: |
| 396 | regexes = { |
| 397 | constants.CRYPTOHOME_FS_REGEX_ANY : |
| 398 | constants.CRYPTOHOME_DEV_REGEX_ANY |
| 399 | } |
barfab@chromium.org | 5c37463 | 2012-04-05 16:50:56 +0200 | [diff] [blame] | 400 | user_mount_info = __get_user_mount_info(user=user, allow_fail=allow_fail) |
| 401 | for mount_info in user_mount_info: |
Gwendal Grignou | 6bad672 | 2016-06-09 16:36:04 -0700 | [diff] [blame] | 402 | # Look at each /proc/../mount lines that match mount point for a given |
| 403 | # user user/system mount (/home/user/.... /home/root/...) |
| 404 | |
| 405 | # We should have at least 3 arguments (source, mount, type of mount) |
| 406 | if len(mount_info) < 3: |
barfab@chromium.org | 5c37463 | 2012-04-05 16:50:56 +0200 | [diff] [blame] | 407 | return False |
Gwendal Grignou | 6bad672 | 2016-06-09 16:36:04 -0700 | [diff] [blame] | 408 | |
| 409 | device_regex = None |
| 410 | for fs_regex in regexes.keys(): |
| 411 | if re.match(fs_regex, mount_info[2]): |
| 412 | device_regex = regexes[fs_regex] |
| 413 | break |
| 414 | |
| 415 | if not device_regex: |
| 416 | # The thrid argument in not the expectd mount point type. |
| 417 | return False |
| 418 | |
| 419 | # Check if the mount source match the device regex: it can be loose, |
| 420 | # (anything) or stricter if we expect guest filesystem. |
| 421 | if not re.match(device_regex, mount_info[0]): |
| 422 | return False |
| 423 | |
Sergey Poromov | 533008f | 2017-10-13 14:07:43 +0200 | [diff] [blame] | 424 | if (re.match(constants.CRYPTOHOME_FS_REGEX_EXT4, mount_info[2]) |
| 425 | and not(re.match(constants.CRYPTOHOME_DEV_REGEX_LOOP_DEVICE, |
| 426 | mount_info[0]))): |
| 427 | # Ephemeral cryptohome uses ext4 mount from a loop device, |
| 428 | # otherwise it should be ext4 crypto. Check there is an encryption |
| 429 | # key for that directory. |
Gwendal Grignou | 6bad672 | 2016-06-09 16:36:04 -0700 | [diff] [blame] | 430 | find_key_cmd_list = ['e4crypt get_policy %s' % (mount_info[1]), |
| 431 | 'cut -d \' \' -f 2'] |
| 432 | key = __run_cmd(' | ' .join(find_key_cmd_list)) |
| 433 | cmd_list = ['keyctl show @s', |
| 434 | 'grep %s' % (key), |
| 435 | 'wc -l'] |
| 436 | out = __run_cmd(' | '.join(cmd_list)) |
| 437 | if int(out) != 1: |
| 438 | return False |
barfab@chromium.org | 5c37463 | 2012-04-05 16:50:56 +0200 | [diff] [blame] | 439 | return True |
Sourav Poddar | 574bd62 | 2010-05-26 14:22:26 +0530 | [diff] [blame] | 440 | |
| 441 | |
barfab@chromium.org | 5c37463 | 2012-04-05 16:50:56 +0200 | [diff] [blame] | 442 | def is_guest_vault_mounted(allow_fail=False): |
Sergey Poromov | 533008f | 2017-10-13 14:07:43 +0200 | [diff] [blame] | 443 | """Check whether a vault is mounted for the guest user. |
Sergey Poromov | d85dce5 | 2017-12-27 11:10:51 +0100 | [diff] [blame] | 444 | It should be a mount of an ext4 partition on a loop device |
| 445 | or be backed by tmpfs. |
Sergey Poromov | 533008f | 2017-10-13 14:07:43 +0200 | [diff] [blame] | 446 | """ |
barfab@chromium.org | 5c37463 | 2012-04-05 16:50:56 +0200 | [diff] [blame] | 447 | return is_vault_mounted( |
Elly Fong-Jones | 6cb26ad | 2013-05-21 12:09:23 -0400 | [diff] [blame] | 448 | user=GUEST_USER_NAME, |
Gwendal Grignou | 6bad672 | 2016-06-09 16:36:04 -0700 | [diff] [blame] | 449 | regexes={ |
Sergey Poromov | d85dce5 | 2017-12-27 11:10:51 +0100 | [diff] [blame] | 450 | # Remove tmpfs support when it becomes unnecessary as all guest |
| 451 | # modes will use ext4 on a loop device. |
Sergey Poromov | 533008f | 2017-10-13 14:07:43 +0200 | [diff] [blame] | 452 | constants.CRYPTOHOME_FS_REGEX_EXT4 : |
| 453 | constants.CRYPTOHOME_DEV_REGEX_LOOP_DEVICE, |
Sergey Poromov | d85dce5 | 2017-12-27 11:10:51 +0100 | [diff] [blame] | 454 | constants.CRYPTOHOME_FS_REGEX_TMPFS : |
| 455 | constants.CRYPTOHOME_DEV_REGEX_GUEST, |
Gwendal Grignou | 6bad672 | 2016-06-09 16:36:04 -0700 | [diff] [blame] | 456 | }, |
barfab@chromium.org | 5c37463 | 2012-04-05 16:50:56 +0200 | [diff] [blame] | 457 | allow_fail=allow_fail) |
| 458 | |
Gwendal Grignou | 6bad672 | 2016-06-09 16:36:04 -0700 | [diff] [blame] | 459 | def is_permanent_vault_mounted(user, allow_fail=False): |
| 460 | """Check if user is mounted over ecryptfs or ext4 crypto. """ |
| 461 | return is_vault_mounted( |
| 462 | user=user, |
| 463 | regexes={ |
| 464 | constants.CRYPTOHOME_FS_REGEX_ECRYPTFS : |
| 465 | constants.CRYPTOHOME_DEV_REGEX_REGULAR_USER_SHADOW, |
| 466 | constants.CRYPTOHOME_FS_REGEX_EXT4 : |
| 467 | constants.CRYPTOHOME_DEV_REGEX_REGULAR_USER_DEVICE, |
| 468 | }, |
| 469 | allow_fail=allow_fail) |
barfab@chromium.org | 5c37463 | 2012-04-05 16:50:56 +0200 | [diff] [blame] | 470 | |
Kazuhiro Inaba | a3bf645 | 2017-02-08 11:41:50 +0900 | [diff] [blame] | 471 | def get_mounted_vault_path(user, allow_fail=False): |
| 472 | """Get the path where the decrypted data for the user is located.""" |
| 473 | return os.path.join(constants.SHADOW_ROOT, get_user_hash(user), 'mount') |
Nirnimesh | 6681449 | 2011-06-27 18:00:33 -0700 | [diff] [blame] | 474 | |
| 475 | |
| 476 | def canonicalize(credential): |
barfab@chromium.org | 5c37463 | 2012-04-05 16:50:56 +0200 | [diff] [blame] | 477 | """Perform basic canonicalization of |email_address|. |
Nirnimesh | 6681449 | 2011-06-27 18:00:33 -0700 | [diff] [blame] | 478 | |
barfab@chromium.org | 5c37463 | 2012-04-05 16:50:56 +0200 | [diff] [blame] | 479 | Perform basic canonicalization of |email_address|, taking into account that |
| 480 | gmail does not consider '.' or caps inside a username to matter. It also |
| 481 | ignores everything after a '+'. For example, |
| 482 | c.masone+abc@gmail.com == cMaSone@gmail.com, per |
Nirnimesh | 6681449 | 2011-06-27 18:00:33 -0700 | [diff] [blame] | 483 | http://mail.google.com/support/bin/answer.py?hl=en&ctx=mail&answer=10313 |
| 484 | """ |
| 485 | if not credential: |
| 486 | return None |
| 487 | |
| 488 | parts = credential.split('@') |
| 489 | if len(parts) != 2: |
barfab@chromium.org | 5c37463 | 2012-04-05 16:50:56 +0200 | [diff] [blame] | 490 | raise error.TestError('Malformed email: ' + credential) |
Nirnimesh | 6681449 | 2011-06-27 18:00:33 -0700 | [diff] [blame] | 491 | |
| 492 | (name, domain) = parts |
| 493 | name = name.partition('+')[0] |
barfab@chromium.org | 5c37463 | 2012-04-05 16:50:56 +0200 | [diff] [blame] | 494 | if (domain == constants.SPECIAL_CASE_DOMAIN): |
Nirnimesh | 6681449 | 2011-06-27 18:00:33 -0700 | [diff] [blame] | 495 | name = name.replace('.', '') |
| 496 | return '@'.join([name, domain]).lower() |
Elly Jones | 686c2f4 | 2011-10-24 16:45:07 -0400 | [diff] [blame] | 497 | |
barfab@chromium.org | cf2151e | 2012-04-04 15:39:34 +0200 | [diff] [blame] | 498 | |
Will Drewry | d2fed97 | 2013-12-05 16:35:51 -0600 | [diff] [blame] | 499 | def crash_cryptohomed(): |
Will Drewry | dc2b0dd | 2013-12-10 16:41:04 -0600 | [diff] [blame] | 500 | # Try to kill cryptohomed so we get something to work with. |
| 501 | pid = __run_cmd('pgrep cryptohomed') |
| 502 | try: |
Will Drewry | 9e44079 | 2013-12-11 17:18:35 -0600 | [diff] [blame] | 503 | pid = int(pid) |
Will Drewry | dc2b0dd | 2013-12-10 16:41:04 -0600 | [diff] [blame] | 504 | except ValueError, e: # empty or invalid string |
Will Drewry | 9e44079 | 2013-12-11 17:18:35 -0600 | [diff] [blame] | 505 | raise error.TestError('Cryptohomed was not running') |
Will Drewry | dc2b0dd | 2013-12-10 16:41:04 -0600 | [diff] [blame] | 506 | utils.system('kill -ABRT %d' % pid) |
| 507 | # CONT just in case cryptohomed had a spurious STOP. |
| 508 | utils.system('kill -CONT %d' % pid) |
| 509 | utils.poll_for_condition( |
| 510 | lambda: utils.system('ps -p %d' % pid, |
| 511 | ignore_status=True) != 0, |
Will Drewry | 934d153 | 2014-01-30 16:23:17 -0600 | [diff] [blame] | 512 | timeout=180, |
Will Drewry | dc2b0dd | 2013-12-10 16:41:04 -0600 | [diff] [blame] | 513 | exception=error.TestError( |
| 514 | 'Timeout waiting for cryptohomed to coredump')) |
| 515 | |
Will Drewry | d2fed97 | 2013-12-05 16:35:51 -0600 | [diff] [blame] | 516 | |
Dan Spaid | be9789c | 2017-05-19 15:18:42 +0900 | [diff] [blame] | 517 | def create_ecryptfs_homedir(user, password): |
| 518 | """Creates a new home directory as ecryptfs. |
| 519 | |
| 520 | If a home directory for the user exists already, it will be removed. |
| 521 | The resulting home directory will be mounted. |
| 522 | |
| 523 | @param user: Username to create the home directory for. |
| 524 | @param password: Password to use when creating the home directory. |
| 525 | """ |
| 526 | unmount_vault(user) |
| 527 | remove_vault(user) |
| 528 | args = [ |
| 529 | CRYPTOHOME_CMD, |
| 530 | '--action=mount_ex', |
| 531 | '--user=%s' % user, |
| 532 | '--password=%s' % password, |
| 533 | '--key_label=foo', |
| 534 | '--ecryptfs', |
| 535 | '--create'] |
| 536 | logging.info(__run_cmd(' '.join(args))) |
| 537 | if not is_vault_mounted(user, regexes={ |
| 538 | constants.CRYPTOHOME_FS_REGEX_ECRYPTFS : |
| 539 | constants.CRYPTOHOME_DEV_REGEX_REGULAR_USER_SHADOW |
| 540 | }, allow_fail=True): |
| 541 | raise ChromiumOSError('Ecryptfs home could not be created') |
| 542 | |
| 543 | |
| 544 | def do_dircrypto_migration(user, password, timeout=600): |
| 545 | """Start dircrypto migration for the user. |
| 546 | |
| 547 | @param user: The user to migrate. |
| 548 | @param password: The password used to mount the users vault |
| 549 | @param timeout: How long in seconds to wait for the migration to finish |
| 550 | before failing. |
| 551 | """ |
| 552 | unmount_vault(user) |
| 553 | args = [ |
| 554 | CRYPTOHOME_CMD, |
| 555 | '--action=mount_ex', |
| 556 | '--to_migrate_from_ecryptfs', |
| 557 | '--user=%s' % user, |
| 558 | '--password=%s' % password] |
| 559 | logging.info(__run_cmd(' '.join(args))) |
| 560 | if not __get_mount_info(temporary_mount_path(user), allow_fail=True): |
| 561 | raise ChromiumOSError('Failed to mount home for migration') |
| 562 | args = [CRYPTOHOME_CMD, '--action=migrate_to_dircrypto', '--user=%s' % user] |
| 563 | logging.info(__run_cmd(' '.join(args))) |
| 564 | utils.poll_for_condition( |
| 565 | lambda: not __get_mount_info( |
| 566 | temporary_mount_path(user), allow_fail=True), |
| 567 | timeout=timeout, |
| 568 | exception=error.TestError( |
| 569 | 'Timeout waiting for dircrypto migration to finish')) |
| 570 | |
| 571 | |
Eric Caruso | 014d5ed | 2018-02-01 16:24:41 -0800 | [diff] [blame] | 572 | def change_password(user, password, new_password): |
| 573 | args = [ |
| 574 | CRYPTOHOME_CMD, |
Greg Kerr | 98aa75c | 2018-06-05 15:27:12 -0700 | [diff] [blame] | 575 | '--action=migrate_key_ex', |
Eric Caruso | 014d5ed | 2018-02-01 16:24:41 -0800 | [diff] [blame] | 576 | '--user=%s' % user, |
| 577 | '--old_password=%s' % password, |
| 578 | '--password=%s' % new_password] |
Eric Caruso | 43ec57e | 2018-02-05 15:54:49 -0800 | [diff] [blame] | 579 | out = __run_cmd(' '.join(args)) |
| 580 | logging.info(out) |
| 581 | if 'Key migration succeeded.' not in out: |
| 582 | raise ChromiumOSError('Key migration failed.') |
Eric Caruso | 014d5ed | 2018-02-01 16:24:41 -0800 | [diff] [blame] | 583 | |
| 584 | |
Will Drewry | 9e44079 | 2013-12-11 17:18:35 -0600 | [diff] [blame] | 585 | class CryptohomeProxy(DBusClient): |
| 586 | """A DBus proxy client for testing the Cryptohome DBus server. |
| 587 | """ |
| 588 | CRYPTOHOME_BUS_NAME = 'org.chromium.Cryptohome' |
| 589 | CRYPTOHOME_OBJECT_PATH = '/org/chromium/Cryptohome' |
| 590 | CRYPTOHOME_INTERFACE = 'org.chromium.CryptohomeInterface' |
| 591 | ASYNC_CALL_STATUS_SIGNAL = 'AsyncCallStatus' |
| 592 | ASYNC_CALL_STATUS_SIGNAL_ARGUMENTS = ( |
| 593 | 'async_id', 'return_status', 'return_code' |
| 594 | ) |
| 595 | DBUS_PROPERTIES_INTERFACE = 'org.freedesktop.DBus.Properties' |
| 596 | |
Lutz Justen | b927578 | 2018-06-20 18:42:22 +0200 | [diff] [blame^] | 597 | # Default timeout in seconds for the D-Bus connection. |
| 598 | DEFAULT_DBUS_TIMEOUT = 30 |
Chris Masone | 19e305e | 2014-03-14 15:13:46 -0700 | [diff] [blame] | 599 | |
Lutz Justen | b927578 | 2018-06-20 18:42:22 +0200 | [diff] [blame^] | 600 | def __init__(self, bus_loop=None, autodir=None, job=None, |
| 601 | timeout=DEFAULT_DBUS_TIMEOUT): |
Eric Caruso | 8dc4098 | 2018-03-20 17:05:19 -0700 | [diff] [blame] | 602 | if autodir and job: |
| 603 | # Install D-Bus protos necessary for some methods. |
| 604 | dep_dir = os.path.join(autodir, 'deps', DBUS_PROTOS_DEP) |
| 605 | job.install_pkg(DBUS_PROTOS_DEP, 'dep', dep_dir) |
| 606 | sys.path.append(dep_dir) |
| 607 | |
| 608 | # Set up D-Bus main loop and interface. |
Will Drewry | 9e44079 | 2013-12-11 17:18:35 -0600 | [diff] [blame] | 609 | self.main_loop = gobject.MainLoop() |
Will Drewry | 78db9dc | 2014-04-01 16:34:23 -0500 | [diff] [blame] | 610 | if bus_loop is None: |
Chris Masone | 64170f8 | 2014-03-14 15:47:05 -0700 | [diff] [blame] | 611 | bus_loop = DBusGMainLoop(set_as_default=True) |
Will Drewry | 9e44079 | 2013-12-11 17:18:35 -0600 | [diff] [blame] | 612 | self.bus = dbus.SystemBus(mainloop=bus_loop) |
| 613 | super(CryptohomeProxy, self).__init__(self.main_loop, self.bus, |
| 614 | self.CRYPTOHOME_BUS_NAME, |
Lutz Justen | 1c6be45 | 2018-05-29 13:37:00 +0200 | [diff] [blame] | 615 | self.CRYPTOHOME_OBJECT_PATH, |
| 616 | timeout) |
Will Drewry | 9e44079 | 2013-12-11 17:18:35 -0600 | [diff] [blame] | 617 | self.iface = dbus.Interface(self.proxy_object, |
| 618 | self.CRYPTOHOME_INTERFACE) |
| 619 | self.properties = dbus.Interface(self.proxy_object, |
| 620 | self.DBUS_PROPERTIES_INTERFACE) |
| 621 | self.handle_signal(self.CRYPTOHOME_INTERFACE, |
| 622 | self.ASYNC_CALL_STATUS_SIGNAL, |
| 623 | self.ASYNC_CALL_STATUS_SIGNAL_ARGUMENTS) |
Elly Jones | 2f0ebba | 2011-10-27 13:43:20 -0400 | [diff] [blame] | 624 | |
Chris Masone | 19e305e | 2014-03-14 15:13:46 -0700 | [diff] [blame] | 625 | |
Will Drewry | d2fed97 | 2013-12-05 16:35:51 -0600 | [diff] [blame] | 626 | # Wrap all proxied calls to catch cryptohomed failures. |
| 627 | def __call(self, method, *args): |
| 628 | try: |
Chris Masone | f59d9df | 2014-03-14 12:05:32 -0700 | [diff] [blame] | 629 | return method(*args, timeout=180) |
Will Drewry | d2fed97 | 2013-12-05 16:35:51 -0600 | [diff] [blame] | 630 | except dbus.exceptions.DBusException, e: |
| 631 | if e.get_dbus_name() == 'org.freedesktop.DBus.Error.NoReply': |
| 632 | logging.error('Cryptohome is not responding. Sending ABRT') |
| 633 | crash_cryptohomed() |
| 634 | raise ChromiumOSError('cryptohomed aborted. Check crashes!') |
| 635 | raise e |
| 636 | |
Chris Masone | 19e305e | 2014-03-14 15:13:46 -0700 | [diff] [blame] | 637 | |
Will Drewry | 9e44079 | 2013-12-11 17:18:35 -0600 | [diff] [blame] | 638 | def __wait_for_specific_signal(self, signal, data): |
| 639 | """Wait for the |signal| with matching |data| |
| 640 | Returns the resulting dict on success or {} on error. |
| 641 | """ |
Will Drewry | c4de5ff | 2014-02-03 13:26:57 -0600 | [diff] [blame] | 642 | # Do not bubble up the timeout here, just return {}. |
| 643 | result = {} |
| 644 | try: |
| 645 | result = self.wait_for_signal(signal) |
| 646 | except utils.TimeoutError: |
| 647 | return {} |
Will Drewry | 9e44079 | 2013-12-11 17:18:35 -0600 | [diff] [blame] | 648 | for k in data.keys(): |
| 649 | if not result.has_key(k) or result[k] != data[k]: |
| 650 | return {} |
| 651 | return result |
| 652 | |
Chris Masone | 19e305e | 2014-03-14 15:13:46 -0700 | [diff] [blame] | 653 | |
Will Drewry | 9e44079 | 2013-12-11 17:18:35 -0600 | [diff] [blame] | 654 | # Perform a data-less async call. |
| 655 | # TODO(wad) Add __async_data_call. |
| 656 | def __async_call(self, method, *args): |
Will Drewry | fef135a | 2014-05-23 16:02:14 -0500 | [diff] [blame] | 657 | # Clear out any superfluous async call signals. |
| 658 | self.clear_signal_content(self.ASYNC_CALL_STATUS_SIGNAL) |
Will Drewry | 9e44079 | 2013-12-11 17:18:35 -0600 | [diff] [blame] | 659 | out = self.__call(method, *args) |
| 660 | logging.debug('Issued call ' + str(method) + |
| 661 | ' with async_id ' + str(out)) |
| 662 | result = {} |
| 663 | try: |
Will Drewry | 934d153 | 2014-01-30 16:23:17 -0600 | [diff] [blame] | 664 | # __wait_for_specific_signal has a 10s timeout |
Will Drewry | 9e44079 | 2013-12-11 17:18:35 -0600 | [diff] [blame] | 665 | result = utils.poll_for_condition( |
| 666 | lambda: self.__wait_for_specific_signal( |
| 667 | self.ASYNC_CALL_STATUS_SIGNAL, {'async_id' : out}), |
Will Drewry | 934d153 | 2014-01-30 16:23:17 -0600 | [diff] [blame] | 668 | timeout=180, |
Will Drewry | 9e44079 | 2013-12-11 17:18:35 -0600 | [diff] [blame] | 669 | desc='matching %s signal' % self.ASYNC_CALL_STATUS_SIGNAL) |
| 670 | except utils.TimeoutError, e: |
| 671 | logging.error('Cryptohome timed out. Sending ABRT.') |
| 672 | crash_cryptohomed() |
| 673 | raise ChromiumOSError('cryptohomed aborted. Check crashes!') |
| 674 | return result |
| 675 | |
Chris Masone | 19e305e | 2014-03-14 15:13:46 -0700 | [diff] [blame] | 676 | |
Eric Caruso | 8dc4098 | 2018-03-20 17:05:19 -0700 | [diff] [blame] | 677 | def mount(self, user, password, create=False, async=True, key_label='bar'): |
Elly Jones | 2f0ebba | 2011-10-27 13:43:20 -0400 | [diff] [blame] | 678 | """Mounts a cryptohome. |
| 679 | |
| 680 | Returns True if the mount succeeds or False otherwise. |
Elly Jones | 2f0ebba | 2011-10-27 13:43:20 -0400 | [diff] [blame] | 681 | """ |
Eric Caruso | 8dc4098 | 2018-03-20 17:05:19 -0700 | [diff] [blame] | 682 | import rpc_pb2 |
| 683 | |
| 684 | acc = rpc_pb2.AccountIdentifier() |
| 685 | acc.account_id = user |
| 686 | |
| 687 | auth = rpc_pb2.AuthorizationRequest() |
| 688 | auth.key.secret = password |
| 689 | auth.key.data.label = key_label |
| 690 | |
| 691 | mount_req = rpc_pb2.MountRequest() |
| 692 | if create: |
| 693 | mount_req.create.copy_authorization_key = True |
| 694 | |
| 695 | out = self.__call(self.iface.MountEx, acc.SerializeToString(), |
| 696 | auth.SerializeToString(), mount_req.SerializeToString()) |
| 697 | parsed_out = rpc_pb2.BaseReply() |
| 698 | parsed_out.ParseFromString(''.join(map(chr, out))) |
| 699 | return parsed_out.error == rpc_pb2.CRYPTOHOME_ERROR_NOT_SET |
Elly Jones | 2f0ebba | 2011-10-27 13:43:20 -0400 | [diff] [blame] | 700 | |
Chris Masone | 19e305e | 2014-03-14 15:13:46 -0700 | [diff] [blame] | 701 | |
Elly Jones | 2f0ebba | 2011-10-27 13:43:20 -0400 | [diff] [blame] | 702 | def unmount(self, user): |
| 703 | """Unmounts a cryptohome. |
| 704 | |
| 705 | Returns True if the unmount suceeds or false otherwise. |
| 706 | TODO(ellyjones): Once there's a per-user unmount method, use it. See |
| 707 | <crosbug.com/20778>. |
| 708 | """ |
Will Drewry | d2fed97 | 2013-12-05 16:35:51 -0600 | [diff] [blame] | 709 | return self.__call(self.iface.Unmount) |
Elly Jones | 2f0ebba | 2011-10-27 13:43:20 -0400 | [diff] [blame] | 710 | |
Chris Masone | 19e305e | 2014-03-14 15:13:46 -0700 | [diff] [blame] | 711 | |
Elly Jones | 2f0ebba | 2011-10-27 13:43:20 -0400 | [diff] [blame] | 712 | def is_mounted(self, user): |
| 713 | """Tests whether a user's cryptohome is mounted.""" |
| 714 | return (utils.is_mountpoint(user_path(user)) |
| 715 | and utils.is_mountpoint(system_path(user))) |
| 716 | |
Chris Masone | 19e305e | 2014-03-14 15:13:46 -0700 | [diff] [blame] | 717 | |
Elly Jones | 2f0ebba | 2011-10-27 13:43:20 -0400 | [diff] [blame] | 718 | def require_mounted(self, user): |
| 719 | """Raises a test failure if a user's cryptohome is not mounted.""" |
| 720 | utils.require_mountpoint(user_path(user)) |
| 721 | utils.require_mountpoint(system_path(user)) |
Elly Jones | 4458f44 | 2012-04-16 15:42:56 -0400 | [diff] [blame] | 722 | |
Chris Masone | 19e305e | 2014-03-14 15:13:46 -0700 | [diff] [blame] | 723 | |
Will Drewry | 9e44079 | 2013-12-11 17:18:35 -0600 | [diff] [blame] | 724 | def migrate(self, user, oldkey, newkey, async=True): |
Elly Jones | 4458f44 | 2012-04-16 15:42:56 -0400 | [diff] [blame] | 725 | """Migrates the specified user's cryptohome from one key to another.""" |
Will Drewry | 9e44079 | 2013-12-11 17:18:35 -0600 | [diff] [blame] | 726 | if async: |
| 727 | return self.__async_call(self.iface.AsyncMigrateKey, |
| 728 | user, oldkey, newkey)['return_status'] |
Will Drewry | d2fed97 | 2013-12-05 16:35:51 -0600 | [diff] [blame] | 729 | return self.__call(self.iface.MigrateKey, user, oldkey, newkey) |
Elly Jones | 4458f44 | 2012-04-16 15:42:56 -0400 | [diff] [blame] | 730 | |
Chris Masone | 19e305e | 2014-03-14 15:13:46 -0700 | [diff] [blame] | 731 | |
Will Drewry | 9e44079 | 2013-12-11 17:18:35 -0600 | [diff] [blame] | 732 | def remove(self, user, async=True): |
| 733 | if async: |
| 734 | return self.__async_call(self.iface.AsyncRemove, |
| 735 | user)['return_status'] |
Will Drewry | d2fed97 | 2013-12-05 16:35:51 -0600 | [diff] [blame] | 736 | return self.__call(self.iface.Remove, user) |
Chris Masone | 19e305e | 2014-03-14 15:13:46 -0700 | [diff] [blame] | 737 | |
| 738 | |
| 739 | def ensure_clean_cryptohome_for(self, user, password=None): |
| 740 | """Ensure a fresh cryptohome exists for user. |
| 741 | |
| 742 | @param user: user who needs a shiny new cryptohome. |
| 743 | @param password: if unset, a random password will be used. |
| 744 | """ |
| 745 | if not password: |
| 746 | password = ''.join(random.sample(string.ascii_lowercase, 6)) |
| 747 | self.remove(user) |
| 748 | self.mount(user, password, create=True) |
Roman Sorokin | a45273e | 2017-12-20 12:03:27 +0100 | [diff] [blame] | 749 | |
| 750 | def lock_install_attributes(self, attrs): |
| 751 | """Set and lock install attributes for the device. |
| 752 | |
| 753 | @param attrs: dict of install attributes. |
| 754 | """ |
Roman Sorokin | 0a228d1 | 2018-01-23 12:36:45 +0100 | [diff] [blame] | 755 | take_tpm_ownership() |
Roman Sorokin | a45273e | 2017-12-20 12:03:27 +0100 | [diff] [blame] | 756 | for key, value in attrs.items(): |
| 757 | if not self.__call(self.iface.InstallAttributesSet, key, |
| 758 | dbus.ByteArray(value + '\0')): |
| 759 | return False |
| 760 | return self.__call(self.iface.InstallAttributesFinalize) |