Skip to content

Commit 18026d8

Browse files
ebiggersdhowells
authored andcommitted
KEYS: reject NULL restriction string when type is specified
keyctl_restrict_keyring() allows through a NULL restriction when the "type" is non-NULL, which causes a NULL pointer dereference in asymmetric_lookup_restriction() when it calls strcmp() on the restriction string. But no key types actually use a "NULL restriction" to mean anything, so update keyctl_restrict_keyring() to reject it with EINVAL. Reported-by: syzbot <[email protected]> Fixes: 97d3aa0 ("KEYS: Add a lookup_restriction function for the asymmetric key type") Cc: <[email protected]> # v4.12+ Signed-off-by: Eric Biggers <[email protected]> Signed-off-by: David Howells <[email protected]>
1 parent 3d1f025 commit 18026d8

File tree

1 file changed

+10
-14
lines changed

1 file changed

+10
-14
lines changed

security/keys/keyctl.c

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1588,17 +1588,15 @@ long keyctl_session_to_parent(void)
15881588
* The caller must have Setattr permission to change keyring restrictions.
15891589
*
15901590
* The requested type name may be a NULL pointer to reject all attempts
1591-
* to link to the keyring. If _type is non-NULL, _restriction can be
1592-
* NULL or a pointer to a string describing the restriction. If _type is
1593-
* NULL, _restriction must also be NULL.
1591+
* to link to the keyring. In this case, _restriction must also be NULL.
1592+
* Otherwise, both _type and _restriction must be non-NULL.
15941593
*
15951594
* Returns 0 if successful.
15961595
*/
15971596
long keyctl_restrict_keyring(key_serial_t id, const char __user *_type,
15981597
const char __user *_restriction)
15991598
{
16001599
key_ref_t key_ref;
1601-
bool link_reject = !_type;
16021600
char type[32];
16031601
char *restriction = NULL;
16041602
long ret;
@@ -1607,31 +1605,29 @@ long keyctl_restrict_keyring(key_serial_t id, const char __user *_type,
16071605
if (IS_ERR(key_ref))
16081606
return PTR_ERR(key_ref);
16091607

1608+
ret = -EINVAL;
16101609
if (_type) {
1611-
ret = key_get_type_from_user(type, _type, sizeof(type));
1612-
if (ret < 0)
1610+
if (!_restriction)
16131611
goto error;
1614-
}
16151612

1616-
if (_restriction) {
1617-
if (!_type) {
1618-
ret = -EINVAL;
1613+
ret = key_get_type_from_user(type, _type, sizeof(type));
1614+
if (ret < 0)
16191615
goto error;
1620-
}
16211616

16221617
restriction = strndup_user(_restriction, PAGE_SIZE);
16231618
if (IS_ERR(restriction)) {
16241619
ret = PTR_ERR(restriction);
16251620
goto error;
16261621
}
1622+
} else {
1623+
if (_restriction)
1624+
goto error;
16271625
}
16281626

1629-
ret = keyring_restrict(key_ref, link_reject ? NULL : type, restriction);
1627+
ret = keyring_restrict(key_ref, _type ? type : NULL, restriction);
16301628
kfree(restriction);
1631-
16321629
error:
16331630
key_ref_put(key_ref);
1634-
16351631
return ret;
16361632
}
16371633

0 commit comments

Comments
 (0)