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