Skip to content

Commit c1f480b

Browse files
blucaaxboe
authored andcommitted
sed-opal: allow using IOC_OPAL_SAVE for locking too
Usually when closing a crypto device (eg: dm-crypt with LUKS) the volume key is not required, as it requires root privileges anyway, and root can deny access to a disk in many ways regardless. Requiring the volume key to lock the device is a peculiarity of the OPAL specification. Given we might already have saved the key if the user requested it via the 'IOC_OPAL_SAVE' ioctl, we can use that key to lock the device if no key was provided here and the locking range matches, and the user sets the appropriate flag with 'IOC_OPAL_SAVE'. This allows integrating OPAL with tools and libraries that are used to the common behaviour and do not ask for the volume key when closing a device. Callers can always pass a non-zero key and it will be used regardless, as before. Suggested-by: Štěpán Horáček <[email protected]> Signed-off-by: Luca Boccassi <[email protected]> Reviewed-by: Christoph Hellwig <[email protected]> Reviewed-by: Christian Brauner <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jens Axboe <[email protected]>
1 parent 3775459 commit c1f480b

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

block/sed-opal.c

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2437,6 +2437,44 @@ static int __opal_set_mbr_done(struct opal_dev *dev, struct opal_key *key)
24372437
return execute_steps(dev, mbrdone_step, ARRAY_SIZE(mbrdone_step));
24382438
}
24392439

2440+
static void opal_lock_check_for_saved_key(struct opal_dev *dev,
2441+
struct opal_lock_unlock *lk_unlk)
2442+
{
2443+
struct opal_suspend_data *iter;
2444+
2445+
if (lk_unlk->l_state != OPAL_LK ||
2446+
lk_unlk->session.opal_key.key_len > 0)
2447+
return;
2448+
2449+
/*
2450+
* Usually when closing a crypto device (eg: dm-crypt with LUKS) the
2451+
* volume key is not required, as it requires root privileges anyway,
2452+
* and root can deny access to a disk in many ways regardless.
2453+
* Requiring the volume key to lock the device is a peculiarity of the
2454+
* OPAL specification. Given we might already have saved the key if
2455+
* the user requested it via the 'IOC_OPAL_SAVE' ioctl, we can use
2456+
* that key to lock the device if no key was provided here, the
2457+
* locking range matches and the appropriate flag was passed with
2458+
* 'IOC_OPAL_SAVE'.
2459+
* This allows integrating OPAL with tools and libraries that are used
2460+
* to the common behaviour and do not ask for the volume key when
2461+
* closing a device.
2462+
*/
2463+
setup_opal_dev(dev);
2464+
list_for_each_entry(iter, &dev->unlk_lst, node) {
2465+
if ((iter->unlk.flags & OPAL_SAVE_FOR_LOCK) &&
2466+
iter->lr == lk_unlk->session.opal_key.lr &&
2467+
iter->unlk.session.opal_key.key_len > 0) {
2468+
lk_unlk->session.opal_key.key_len =
2469+
iter->unlk.session.opal_key.key_len;
2470+
memcpy(lk_unlk->session.opal_key.key,
2471+
iter->unlk.session.opal_key.key,
2472+
iter->unlk.session.opal_key.key_len);
2473+
break;
2474+
}
2475+
}
2476+
}
2477+
24402478
static int opal_lock_unlock(struct opal_dev *dev,
24412479
struct opal_lock_unlock *lk_unlk)
24422480
{
@@ -2446,6 +2484,7 @@ static int opal_lock_unlock(struct opal_dev *dev,
24462484
return -EINVAL;
24472485

24482486
mutex_lock(&dev->dev_lock);
2487+
opal_lock_check_for_saved_key(dev, lk_unlk);
24492488
ret = __opal_lock_unlock(dev, lk_unlk);
24502489
mutex_unlock(&dev->dev_lock);
24512490

include/uapi/linux/sed-opal.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ enum opal_lock_state {
4444
OPAL_LK = 0x04, /* 0100 */
4545
};
4646

47+
enum opal_lock_flags {
48+
/* IOC_OPAL_SAVE will also store the provided key for locking */
49+
OPAL_SAVE_FOR_LOCK = 0x01,
50+
};
51+
4752
struct opal_key {
4853
__u8 lr;
4954
__u8 key_len;
@@ -76,7 +81,8 @@ struct opal_user_lr_setup {
7681
struct opal_lock_unlock {
7782
struct opal_session_info session;
7883
__u32 l_state;
79-
__u8 __align[4];
84+
__u16 flags;
85+
__u8 __align[2];
8086
};
8187

8288
struct opal_new_pw {

0 commit comments

Comments
 (0)