Skip to contentSkip to conversation

crypto: ccp - Limit memory allocation in SEV_GET_ID2 ioctl

PATCH In review Peter Gonda 4 participants 12 messages Dec 14, 2022 – Jan 6, 2023

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 crypto: ccp - Limit memory allocation in SEV_GET_ID2 ioctl

Currently userspace can ask for any uint32 size allocation for the SEV_GET_ID2. Limit this allocation size to the max physically contiguously allocation: MAX_ORDER.

---
 drivers/crypto/ccp/sev-dev.c | 4 ++++
 1 file changed, 4 insertions(+)
drivers/crypto/ccp/sev-dev.c +4 -0
index 06fc7156c04f..5c16c4406764 100644
@@ -878,6 +878,10 @@ static int sev_ioctl_do_get_id2(struct sev_issue_cmd *argp)
 	if (copy_from_user(&input, (void __user *)argp->data, sizeof(input)))
 		return -EFAULT;
 
+	/* Max length that can be allocated physically contiguously */
+	if (get_order(input.length) >= MAX_ORDER)
+		return -ENOMEM;
+
 	input_address = (void __user *)input.address;
 
 	if (input.address && input.length) {
Signature · 1 line
2.39.0.rc1.256.g54fd8350bd-goog
maintainer
#1 lore raw
Quoted 3 lines · Peter Gonda

Currently userspace can ask for any uint32 size allocation for the SEV_GET_ID2. Limit this allocation size to the max physically contiguously allocation: MAX_ORDER.

This is just to silence the alloc_pages warning, right? If so how about adding __GFP_NOWARN instead?

Cheers,

Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#2 lore raw
Quoted 8 lines · Herbert Xu
Quoted 3 lines · Peter Gonda

Currently userspace can ask for any uint32 size allocation for the SEV_GET_ID2. Limit this allocation size to the max physically contiguously allocation: MAX_ORDER.

This is just to silence the alloc_pages warning, right? If so how about adding __GFP_NOWARN instead?

The goal was to be more explicit about that, but setting __GFP_NOWARN would result in the same functional behavior. If we're to go that route, it would likely be best to add a comment about the limitation.

That said, if AMD would prefer this to be an EINVAL instead of a ENOMEM by introducing a more formal limitation on the length that can be used, that would be preferred so that we don't need to rely on the page allocator's max length to enforce this arbitrarily.

maintainer
#3 lore raw
Quoted 9 lines · David Rientjes

The goal was to be more explicit about that, but setting __GFP_NOWARN would result in the same functional behavior. If we're to go that route, it would likely be best to add a comment about the limitation.

That said, if AMD would prefer this to be an EINVAL instead of a ENOMEM by introducing a more formal limitation on the length that can be used, that would be preferred so that we don't need to rely on the page allocator's max length to enforce this arbitrarily.

Ideally the limit should be set according to the object that you're trying to allocate. But if that is truly unlimited, and you don't want to see a warning, then GFP_NOWARN seems to fit the bill.

Thanks,

Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
#4 lore raw
Quoted 11 lines · Herbert Xu
Quoted 9 lines · David Rientjes

The goal was to be more explicit about that, but setting __GFP_NOWARN would result in the same functional behavior. If we're to go that route, it would likely be best to add a comment about the limitation.

That said, if AMD would prefer this to be an EINVAL instead of a ENOMEM by introducing a more formal limitation on the length that can be used, that would be preferred so that we don't need to rely on the page allocator's max length to enforce this arbitrarily.

Quoted 5 lines

Ideally the limit should be set according to the object that you're trying to allocate. But if that is truly unlimited, and you don't want to see a warning, then GFP_NOWARN seems to fit the bill.

AMD would be able to speak authoritatively on it, but I think the length of the ID isn't to be assumed by software because it will likely change later.

I don't think there's an active vulnerability with the currnet code so we can likely drop [email protected] for this. The kzalloc() will fail if you try to allocate over 2MB. If you try to allocate >32KB, the page allocator will attempt to reclaim but won't oom kill. If you try to allocate <=32KB, there's the potential for oom kill if nothing is reclaimable, but if memory is that scarce I think we have bigger problems.

So __GFP_NOWARN will work, but I also think it's subtle enough that it warrants being coupled with a comment.

#5 lore raw

PATCH 1/1 crypto: ccp - Avoid page allocation failure warning for SEV_GET_ID2

For SEV_GET_ID2, the user provided length does not have a specified limitation because the length of the ID may change in the future. The kernel memory allocation, however, is implicitly limited to 4MB on x86 by the page allocator, otherwise the kzalloc() will fail.

When this happens, it is best not to spam the kernel log with the warning. Simply fail the allocation and return ENOMEM to the user.

Fixes d6112ea0cb34 ("crypto: ccp - introduce SEV_GET_ID2 command")
---
 drivers/crypto/ccp/sev-dev.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
drivers/crypto/ccp/sev-dev.c +8 -1
@@ -881,7 +881,14 @@ static int sev_ioctl_do_get_id2(struct sev_issue_cmd *argp)
 	input_address = (void __user *)input.address;
 
 	if (input.address && input.length) {
-		id_blob = kzalloc(input.length, GFP_KERNEL);
+		/*
+		 * The length of the ID shouldn't be assumed by software since
+		 * it may change in the future.  The allocation size is limited
+		 * to 1 << (PAGE_SHIFT + MAX_ORDER - 1) by the page allocator.
+		 * If the allocation fails, simply return ENOMEM rather than
+		 * warning in the kernel log.
+		 */
+		id_blob = kzalloc(input.length, GFP_KERNEL | __GFP_NOWARN);
 		if (!id_blob)
 			return -ENOMEM;
 
maintainer
#6 lore raw

Re: [patch] crypto: ccp - Avoid page allocation failure warning for SEV_GET_ID2

Quoted 23 lines · David Rientjes

For SEV_GET_ID2, the user provided length does not have a specified limitation because the length of the ID may change in the future. The kernel memory allocation, however, is implicitly limited to 4MB on x86 by the page allocator, otherwise the kzalloc() will fail.

When this happens, it is best not to spam the kernel log with the warning. Simply fail the allocation and return ENOMEM to the user.

Fixes d6112ea0cb34 ("crypto: ccp - introduce SEV_GET_ID2 command")
---
  drivers/crypto/ccp/sev-dev.c | 9 ++++++++-
  1 file changed, 8 insertions(+), 1 deletion(-)
drivers/crypto/ccp/sev-dev.c +0 -0
@@ -881,7 +881,14 @@ static int sev_ioctl_do_get_id2(struct sev_issue_cmd *argp)
  	input_address = (void __user *)input.address;
 

drivers/crypto/ccp/sev-dev.c static int sev_ioctl_do_get_id2(struct sev_issue_cmd *argp) · line 891

Quoted 10 lines
drivers/crypto/ccp/sev-dev.c +8 -1
  	if (input.address && input.length) {
-		id_blob = kzalloc(input.length, GFP_KERNEL);
+		/*
+		 * The length of the ID shouldn't be assumed by software since
+		 * it may change in the future.  The allocation size is limited
+		 * to 1 << (PAGE_SHIFT + MAX_ORDER - 1) by the page allocator.
+		 * If the allocation fails, simply return ENOMEM rather than
+		 * warning in the kernel log.
+		 */
+		id_blob = kzalloc(input.length, GFP_KERNEL | __GFP_NOWARN);

We could do this or we could have the driver invoke the API with a zero length to get the minimum buffer size needed for the call. The driver could then perform some validation checks comparing the supplied input.length to the returned length. If the driver can proceed, then if input.length is exactly 2x the minimum length, then kzalloc the 2 * minimum length, otherwise kzalloc the minimum length. This is a bit more complicated, though, compared to this fix.

Either way, is fine with me. Thoughts?

Thanks,
Tom

Quoted 3 lines
drivers/crypto/ccp/sev-dev.c +0 -0
  		if (!id_blob)
  			return -ENOMEM;
 
#7 lore raw

Re: [patch] crypto: ccp - Avoid page allocation failure warning for SEV_GET_ID2

Quoted 34 lines · Tom Lendacky
Quoted 33 lines · David Rientjes

For SEV_GET_ID2, the user provided length does not have a specified limitation because the length of the ID may change in the future. The kernel memory allocation, however, is implicitly limited to 4MB on x86 by the page allocator, otherwise the kzalloc() will fail.

When this happens, it is best not to spam the kernel log with the warning. Simply fail the allocation and return ENOMEM to the user.

Fixes d6112ea0cb34 ("crypto: ccp - introduce SEV_GET_ID2 command")
---
  drivers/crypto/ccp/sev-dev.c | 9 ++++++++-
  1 file changed, 8 insertions(+), 1 deletion(-)
drivers/crypto/ccp/sev-dev.c +3 -1
@@ -881,7 +881,14 @@ static int sev_ioctl_do_get_id2(struct sev_issue_cmd
 *argp)
  	input_address = (void __user *)input.address;
    	if (input.address && input.length) {
-		id_blob = kzalloc(input.length, GFP_KERNEL);
+		/*
+		 * The length of the ID shouldn't be assumed by software since
+		 * it may change in the future.  The allocation size is

limited

drivers/crypto/ccp/sev-dev.c +4 -0
+		 * to 1 << (PAGE_SHIFT + MAX_ORDER - 1) by the page allocator.
+		 * If the allocation fails, simply return ENOMEM rather than
+		 * warning in the kernel log.
+		 */
Quoted 9 lines

drivers/crypto/ccp/sev-dev.c static int sev_ioctl_do_get_id2(struct sev_issue_cmd *argp) · line 891

Quoted 1 line
drivers/crypto/ccp/sev-dev.c +1 -0
+		id_blob = kzalloc(input.length, GFP_KERNEL | __GFP_NOWARN);

We could do this or we could have the driver invoke the API with a zero length to get the minimum buffer size needed for the call. The driver could then perform some validation checks comparing the supplied input.length to the returned length. If the driver can proceed, then if input.length is exactly 2x the minimum length, then kzalloc the 2 * minimum length, otherwise kzalloc the minimum length. This is a bit more complicated, though, compared to this fix.

Thanks Tom. IIUC, this could be useful to identify situations where input.length != min_length and input.length != min_length*2 and, in those cases, return EINVAL? Or are there situations where this is actually a valid input.length?

I was assuming that the user was always doing its own SEV_GET_ID2 first to determine the length and then use it for input.length, but perhaps that's not the case and they are passing a bogus value.

maintainer
#8 lore raw

Re: [patch] crypto: ccp - Avoid page allocation failure warning for SEV_GET_ID2

Quoted 46 lines · David Rientjes
Quoted 34 lines · Tom Lendacky
Quoted 33 lines · David Rientjes

For SEV_GET_ID2, the user provided length does not have a specified limitation because the length of the ID may change in the future. The kernel memory allocation, however, is implicitly limited to 4MB on x86 by the page allocator, otherwise the kzalloc() will fail.

When this happens, it is best not to spam the kernel log with the warning. Simply fail the allocation and return ENOMEM to the user.

Fixes d6112ea0cb34 ("crypto: ccp - introduce SEV_GET_ID2 command")
---
   drivers/crypto/ccp/sev-dev.c | 9 ++++++++-
   1 file changed, 8 insertions(+), 1 deletion(-)
drivers/crypto/ccp/sev-dev.c +3 -1
@@ -881,7 +881,14 @@ static int sev_ioctl_do_get_id2(struct sev_issue_cmd
 *argp)
   	input_address = (void __user *)input.address;
     	if (input.address && input.length) {
-		id_blob = kzalloc(input.length, GFP_KERNEL);
+		/*
+		 * The length of the ID shouldn't be assumed by software since
+		 * it may change in the future.  The allocation size is

limited

drivers/crypto/ccp/sev-dev.c +4 -0
+		 * to 1 << (PAGE_SHIFT + MAX_ORDER - 1) by the page allocator.
+		 * If the allocation fails, simply return ENOMEM rather than
+		 * warning in the kernel log.
+		 */
Quoted 9 lines

drivers/crypto/ccp/sev-dev.c static int sev_ioctl_do_get_id2(struct sev_issue_cmd *argp) · line 891

Quoted 1 line
drivers/crypto/ccp/sev-dev.c +1 -0
+		id_blob = kzalloc(input.length, GFP_KERNEL | __GFP_NOWARN);

We could do this or we could have the driver invoke the API with a zero length to get the minimum buffer size needed for the call. The driver could then perform some validation checks comparing the supplied input.length to the returned length. If the driver can proceed, then if input.length is exactly 2x the minimum length, then kzalloc the 2 * minimum length, otherwise kzalloc the minimum length. This is a bit more complicated, though, compared to this fix.

Quoted 8 lines

Thanks Tom. IIUC, this could be useful to identify situations where input.length != min_length and input.length != min_length*2 and, in those cases, return EINVAL? Or are there situations where this is actually a valid input.length?

I was assuming that the user was always doing its own SEV_GET_ID2 first to determine the length and then use it for input.length, but perhaps that's not the case and they are passing a bogus value.

Except that if the user was always doing that, then we wouldn't be worried about this case then. But, I think my method is overkill and the simple approach of this patch is the way to go.

Thanks,
Tom

#9 lore raw

Re: [patch] crypto: ccp - Avoid page allocation failure warning for SEV_GET_ID2

Quoted 55 lines · Tom Lendacky
Quoted 55 lines
Quoted 40 lines
Quoted 32 lines

For SEV_GET_ID2, the user provided length does not have a specified limitation because the length of the ID may change in the future. The kernel memory allocation, however, is implicitly limited to 4MB on x86 by
the page allocator, otherwise the kzalloc() will fail.

When this happens, it is best not to spam the kernel log with the warning.
Simply fail the allocation and return ENOMEM to the user.

Fixes d6112ea0cb34 ("crypto: ccp - introduce SEV_GET_ID2 command")
---
   drivers/crypto/ccp/sev-dev.c | 9 ++++++++-
   1 file changed, 8 insertions(+), 1 deletion(-)
drivers/crypto/ccp/sev-dev.c +0 -0
@@ -881,7 +881,14 @@ static int sev_ioctl_do_get_id2(struct

sev_issue_cmd
*argp)

   	input_address = (void __user *)input.address;
     	if (input.address && input.length) {
drivers/crypto/ccp/sev-dev.c +3 -1
-		id_blob = kzalloc(input.length, GFP_KERNEL);
+		/*
+		 * The length of the ID shouldn't be assumed by software since
+		 * it may change in the future.  The allocation size is

limited

drivers/crypto/ccp/sev-dev.c static int sev_ioctl_do_get_id2(struct sev_issue_cmd *argp) · line 891

Quoted 5 lines
drivers/crypto/ccp/sev-dev.c +5 -0
+		 * to 1 << (PAGE_SHIFT + MAX_ORDER - 1) by the page allocator.
+		 * If the allocation fails, simply return ENOMEM rather than
+		 * warning in the kernel log.
+		 */
+		id_blob = kzalloc(input.length, GFP_KERNEL | __GFP_NOWARN);

We could do this or we could have the driver invoke the API with a zero length

Quoted 9 lines

to get the minimum buffer size needed for the call. The driver could then perform some validation checks comparing the supplied input.length to the returned length. If the driver can proceed, then if input.length is exactly 2x
the minimum length, then kzalloc the 2 * minimum length, otherwise kzalloc the
minimum length. This is a bit more complicated, though, compared to this fix.

Thanks Tom. IIUC, this could be useful to identify situations where input.length != min_length and input.length != min_length*2 and, in those cases, return EINVAL? Or are there situations where this is actually a valid input.length?

Quoted 8 lines
Quoted 3 lines

I was assuming that the user was always doing its own SEV_GET_ID2 first to determine the length and then use it for input.length, but perhaps that's not the case and they are passing a bogus value.

Except that if the user was always doing that, then we wouldn't be worried about this case then. But, I think my method is overkill and the simple approach of this patch is the way to go.

Makes sense, thanks for the clarification. Does that translate into an acked-by? :)

maintainer Acked-by
#10 lore raw

Re: [patch] crypto: ccp - Avoid page allocation failure warning for SEV_GET_ID2

Quoted 61 lines · David Rientjes
Quoted 59 lines · Tom Lendacky
Quoted 58 lines
Quoted 40 lines
Quoted 32 lines

For SEV_GET_ID2, the user provided length does not have a specified limitation because the length of the ID may change in the future. The kernel memory allocation, however, is implicitly limited to 4MB on x86 by
the page allocator, otherwise the kzalloc() will fail.

When this happens, it is best not to spam the kernel log with the warning.
Simply fail the allocation and return ENOMEM to the user.

Fixes d6112ea0cb34 ("crypto: ccp - introduce SEV_GET_ID2 command")
---
    drivers/crypto/ccp/sev-dev.c | 9 ++++++++-
    1 file changed, 8 insertions(+), 1 deletion(-)
drivers/crypto/ccp/sev-dev.c +0 -0
@@ -881,7 +881,14 @@ static int sev_ioctl_do_get_id2(struct

sev_issue_cmd
*argp)

    	input_address = (void __user *)input.address;
      	if (input.address && input.length) {
drivers/crypto/ccp/sev-dev.c +3 -1
-		id_blob = kzalloc(input.length, GFP_KERNEL);
+		/*
+		 * The length of the ID shouldn't be assumed by software since
+		 * it may change in the future.  The allocation size is

limited

drivers/crypto/ccp/sev-dev.c static int sev_ioctl_do_get_id2(struct sev_issue_cmd *argp) · line 891

Quoted 5 lines
drivers/crypto/ccp/sev-dev.c +5 -0
+		 * to 1 << (PAGE_SHIFT + MAX_ORDER - 1) by the page allocator.
+		 * If the allocation fails, simply return ENOMEM rather than
+		 * warning in the kernel log.
+		 */
+		id_blob = kzalloc(input.length, GFP_KERNEL | __GFP_NOWARN);

We could do this or we could have the driver invoke the API with a zero length

Quoted 9 lines

to get the minimum buffer size needed for the call. The driver could then perform some validation checks comparing the supplied input.length to the returned length. If the driver can proceed, then if input.length is exactly 2x
the minimum length, then kzalloc the 2 * minimum length, otherwise kzalloc the
minimum length. This is a bit more complicated, though, compared to this fix.

Thanks Tom. IIUC, this could be useful to identify situations where input.length != min_length and input.length != min_length*2 and, in those cases, return EINVAL? Or are there situations where this is actually a valid input.length?

I was assuming that the user was always doing its own SEV_GET_ID2 first to determine the length and then use it for input.length, but perhaps that's not the case and they are passing a bogus value.

Quoted 7 lines
Quoted 4 lines

Except that if the user was always doing that, then we wouldn't be worried about this case then. But, I think my method is overkill and the simple approach of this patch is the way to go.

Makes sense, thanks for the clarification. Does that translate into an acked-by? :)

Ah, yeah, forgot about that!

maintainer
#11 lore raw

Re: [patch] crypto: ccp - Avoid page allocation failure warning for SEV_GET_ID2

In reply to David Rientjes
Quoted 8 lines · David Rientjes

For SEV_GET_ID2, the user provided length does not have a specified limitation because the length of the ID may change in the future. The kernel memory allocation, however, is implicitly limited to 4MB on x86 by the page allocator, otherwise the kzalloc() will fail.

When this happens, it is best not to spam the kernel log with the warning. Simply fail the allocation and return ENOMEM to the user.

Quoted 8 lines
Fixes d6112ea0cb34 ("crypto: ccp - introduce SEV_GET_ID2 command")
---
 drivers/crypto/ccp/sev-dev.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

Patch applied. Thanks.

Signature · 3 lines
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt