Skip to contentSkip to conversation

mm/hugetlb: Fix use after free when subpool max_hpages accounting is not enabled

PATCH Applied Miaohe Lin 3 participants 5 messages Jan 26, 2021 – Jan 28, 2021

Subsystems (2)
lore
Share & tools
lore

Miaohe Lin

author
#0 lore raw

PATCH 1/1 mm/hugetlb: Fix use after free when subpool max_hpages accounting is not enabled

When subpool max_hpages accounting is not enabled, used_hpages is always 0 and might lead to release subpool prematurely because it indicates no pages are used now while there might be.

In order to fix this issue, we should check used_hpages == 0 iff max_hpages accounting is enabled. As max_hpages accounting should be enabled in most common case, this is not worth a Cc stable.

---
 mm/hugetlb.c | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)
mm/hugetlb.c +13 -3
index 777bc0e45bf3..53ea65d1c5ab 100644
@@ -97,16 +97,26 @@ static inline void ClearPageHugeFreed(struct page *head)
 /* Forward declaration */
 static int hugetlb_acct_memory(struct hstate *h, long delta);
 
-static inline void unlock_or_release_subpool(struct hugepage_subpool *spool)
+static inline bool subpool_is_free(struct hugepage_subpool *spool)
 {
-	bool free = (spool->count == 0) && (spool->used_hpages == 0);
+	if (spool->count)
+		return false;
+	if (spool->max_hpages != -1)
+		return spool->used_hpages == 0;
+	if (spool->min_hpages != -1)
+		return spool->rsv_hpages == spool->min_hpages;
 
+	return true;
+}
+
+static inline void unlock_or_release_subpool(struct hugepage_subpool *spool)
+{
 	spin_unlock(&spool->lock);
 
 	/* If no pages are used, and no other handles to the subpool
 	 * remain, give up any reservations based on minimum size and
 	 * free the subpool */
-	if (free) {
+	if (subpool_is_free(spool)) {
 		if (spool->min_hpages != -1)
 			hugetlb_acct_memory(spool->hstate,
 						-spool->min_hpages);
Signature · 1 line
2.19.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 3 lines · Miaohe Lin

When subpool max_hpages accounting is not enabled, used_hpages is always 0 and might lead to release subpool prematurely because it indicates no pages are used now while there might be.

It might be good to say that you need min_hpages accounting (min_size mount option) enabled for this issue to occur. Or, perhaps say this is possible if a hugetlbfs filesystem is created with the min_size option and without the size option.

That might better explain the conditions in which a user could see the issue.

Quoted 3 lines

In order to fix this issue, we should check used_hpages == 0 iff max_hpages accounting is enabled. As max_hpages accounting should be enabled in most common case, this is not worth a Cc stable.

I agree that such a combination of mount options is very uncommon.

Quoted 6 lines
---
 mm/hugetlb.c | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)

Thanks,

Signature · 1 line
Mike Kravetz
Quoted 36 lines
mm/hugetlb.c +13 -3
index 777bc0e45bf3..53ea65d1c5ab 100644
@@ -97,16 +97,26 @@ static inline void ClearPageHugeFreed(struct page *head)
 /* Forward declaration */
 static int hugetlb_acct_memory(struct hstate *h, long delta);
 
-static inline void unlock_or_release_subpool(struct hugepage_subpool *spool)
+static inline bool subpool_is_free(struct hugepage_subpool *spool)
 {
-	bool free = (spool->count == 0) && (spool->used_hpages == 0);
+	if (spool->count)
+		return false;
+	if (spool->max_hpages != -1)
+		return spool->used_hpages == 0;
+	if (spool->min_hpages != -1)
+		return spool->rsv_hpages == spool->min_hpages;
 
+	return true;
+}
+
+static inline void unlock_or_release_subpool(struct hugepage_subpool *spool)
+{
 	spin_unlock(&spool->lock);
 
 	/* If no pages are used, and no other handles to the subpool
 	 * remain, give up any reservations based on minimum size and
 	 * free the subpool */
-	if (free) {
+	if (subpool_is_free(spool)) {
 		if (spool->min_hpages != -1)
 			hugetlb_acct_memory(spool->hstate,
 						-spool->min_hpages);
author
#2 lore raw
Quoted 11 lines · Mike Kravetz
Quoted 3 lines · Miaohe Lin

When subpool max_hpages accounting is not enabled, used_hpages is always 0 and might lead to release subpool prematurely because it indicates no pages are used now while there might be.

It might be good to say that you need min_hpages accounting (min_size mount option) enabled for this issue to occur. Or, perhaps say this is possible if a hugetlbfs filesystem is created with the min_size option and without the size option.

That might better explain the conditions in which a user could see the issue.

So commit log might looks like this ?
"""
If a hugetlbfs filesystem is created with the min_size option and without the size option, used_hpages is always 0 and might lead to release subpool prematurely because it indicates no pages are used now while there might be.

In order to fix this issue, we should check used_hpages == 0 iff max_hpages accounting is enabled. As max_hpages accounting should be enabled in most common case, this is not worth a Cc stable.
"""

If so, should I send a V2 or Andrew would kindly do this? Many thanks.

Quoted 14 lines
Quoted 4 lines

In order to fix this issue, we should check used_hpages == 0 iff max_hpages accounting is enabled. As max_hpages accounting should be enabled in most common case, this is not worth a Cc stable.

I agree that such a combination of mount options is very uncommon.

Quoted 6 lines
---
 mm/hugetlb.c | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)
Quoted 4 lines

Thanks,

Many thanks for review.

maintainer
#3 lore raw
Quoted 20 lines · On Wed, 27 Jan 2021 10:08:22 +0800 Miaohe Lin <[email protected]> wrote:
Quoted 11 lines · Mike Kravetz
Quoted 3 lines · Miaohe Lin

When subpool max_hpages accounting is not enabled, used_hpages is always 0 and might lead to release subpool prematurely because it indicates no pages are used now while there might be.

It might be good to say that you need min_hpages accounting (min_size mount option) enabled for this issue to occur. Or, perhaps say this is possible if a hugetlbfs filesystem is created with the min_size option and without the size option.

That might better explain the conditions in which a user could see the issue.

So commit log might looks like this ?
"""
If a hugetlbfs filesystem is created with the min_size option and without the size option, used_hpages is always 0 and might lead to release subpool prematurely because it indicates no pages are used now while there might be.

Quoted 7 lines

In order to fix this issue, we should check used_hpages == 0 iff max_hpages accounting is enabled. As max_hpages accounting should be enabled in most common case, this is not worth a Cc stable.
"""

If so, should I send a V2 or Andrew would kindly do this? Many thanks.

I made that change, thanks.

author
#4 lore raw

Hi:

Quoted 27 lines · Andrew Morton
Quoted 25 lines · On Wed, 27 Jan 2021 10:08:22 +0800 Miaohe Lin <[email protected]> wrote:
Quoted 11 lines · Mike Kravetz
Quoted 3 lines · Miaohe Lin

When subpool max_hpages accounting is not enabled, used_hpages is always 0 and might lead to release subpool prematurely because it indicates no pages are used now while there might be.

It might be good to say that you need min_hpages accounting (min_size mount option) enabled for this issue to occur. Or, perhaps say this is possible if a hugetlbfs filesystem is created with the min_size option and without the size option.

That might better explain the conditions in which a user could see the issue.

So commit log might looks like this ?
"""
If a hugetlbfs filesystem is created with the min_size option and without the size option, used_hpages is always 0 and might lead to release subpool prematurely because it indicates no pages are used now while there might be.

In order to fix this issue, we should check used_hpages == 0 iff max_hpages accounting is enabled. As max_hpages accounting should be enabled in most common case, this is not worth a Cc stable.
"""

Quoted 4 lines
Quoted 2 lines

If so, should I send a V2 or Andrew would kindly do this? Many thanks.

I made that change, thanks.>

It's very kind of you. Many thanks.:)

Quoted 2 lines

.