Skip to content

Commit 476758e

Browse files
committed
fix: drive encryption detection treated MODE SENSE success as failure
sg_modesense returns the transferred byte count (> 0) on success, but is_ame and sg_set_key compared the result against 0/DEVICE_GOOD. is_ame therefore always reported the drive as non-AME, and sg_set_key bailed out before issuing SECURITY PROTOCOL OUT, so setting a data key always failed on encrypting drives. Compare against < 0 like the other sg_modesense callers, and normalize sg_set_key's success return. Not verified on encrypting tape hardware; the logic follows the documented sg_modesense return convention.
1 parent f3fb8cc commit 476758e

1 file changed

Lines changed: 6 additions & 3 deletions

File tree

src/tape_drivers/linux/sg/sg_tape.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4630,7 +4630,9 @@ static bool is_ame(void *device)
46304630
unsigned char buf[TC_MP_READ_WRITE_CTRL_SIZE] = {0};
46314631
const int ret = sg_modesense(device, TC_MP_READ_WRITE_CTRL, TC_MP_PC_CURRENT, 0, buf, sizeof(buf));
46324632

4633-
if (ret != 0) {
4633+
/* sg_modesense returns the transferred byte count (> 0) on success and a
4634+
* negative error code on failure. */
4635+
if (ret < 0) {
46344636
char message[100] = {0};
46354637
sprintf(message, "failed to get MP %02Xh (%d)", TC_MP_READ_WRITE_CTRL, ret);
46364638
ltfsmsg(LTFS_DEBUG, 30392D, __FUNCTION__, message);
@@ -4720,7 +4722,7 @@ int sg_set_key(void *device, const unsigned char *keyalias, const unsigned char
47204722

47214723
unsigned char buf[TC_MP_READ_WRITE_CTRL_SIZE] = {0};
47224724
ret = sg_modesense(device, TC_MP_READ_WRITE_CTRL, TC_MP_PC_CURRENT, 0, buf, sizeof(buf));
4723-
if (ret != DEVICE_GOOD)
4725+
if (ret < 0) /* sg_modesense returns a byte count (> 0) on success */
47244726
goto out;
47254727

47264728
ltfs_u16tobe(buffer + 0, sps);
@@ -4767,8 +4769,9 @@ int sg_set_key(void *device, const unsigned char *keyalias, const unsigned char
47674769

47684770
memset(buf, 0, sizeof(buf));
47694771
ret = sg_modesense(device, TC_MP_READ_WRITE_CTRL, TC_MP_PC_CURRENT, 0, buf, sizeof(buf));
4770-
if (ret != DEVICE_GOOD)
4772+
if (ret < 0) /* sg_modesense returns a byte count (> 0) on success */
47714773
goto out;
4774+
ret = DEVICE_GOOD; /* normalize the byte count to a success code */
47724775

47734776
free:
47744777
free(buffer);

0 commit comments

Comments
 (0)