Skip to content
This repository was archived by the owner on Nov 21, 2022. It is now read-only.

Commit 4629ad0

Browse files
fukasawa-tsfrothwell
authored andcommitted
/proc/kpageflags: prevent an integer overflow in stable_page_flags()
stable_page_flags() returns kpageflags info in u64, but it uses "1 << KPF_*" internally which is considered as int. This type mismatch causes no visible problem now, but it will if you set bit 32 or more as done in a subsequent patch. So use BIT_ULL in order to avoid future overflow issues. Link: http://lkml.kernel.org/r/20190725023100.31141-2-t-fukasawa@vx.jp.nec.com Signed-off-by: Toshiki Fukasawa <t-fukasawa@vx.jp.nec.com> Cc: Michal Hocko <mhocko@kernel.org> Cc: Dan Williams <dan.j.williams@intel.com> Cc: Alexey Dobriyan <adobriyan@gmail.com> Cc: Christoph Hellwig <hch@lst.de> Cc: Naoya Horiguchi <n-horiguchi@ah.jp.nec.com> Cc: Junichi Nomura <j-nomura@ce.jp.nec.com> Cc: <stable@vger.kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Stephen Rothwell <sfr@canb.auug.org.au>
1 parent 772cd08 commit 4629ad0

File tree

1 file changed

+18
-19
lines changed

1 file changed

+18
-19
lines changed

fs/proc/page.c

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ u64 stable_page_flags(struct page *page)
115115
* it differentiates a memory hole from a page with no flags
116116
*/
117117
if (!page)
118-
return 1 << KPF_NOPAGE;
118+
return BIT_ULL(KPF_NOPAGE);
119119

120120
k = page->flags;
121121
u = 0;
@@ -127,22 +127,22 @@ u64 stable_page_flags(struct page *page)
127127
* simple test in page_mapped() is not enough.
128128
*/
129129
if (!PageSlab(page) && page_mapped(page))
130-
u |= 1 << KPF_MMAP;
130+
u |= BIT_ULL(KPF_MMAP);
131131
if (PageAnon(page))
132-
u |= 1 << KPF_ANON;
132+
u |= BIT_ULL(KPF_ANON);
133133
if (PageKsm(page))
134-
u |= 1 << KPF_KSM;
134+
u |= BIT_ULL(KPF_KSM);
135135

136136
/*
137137
* compound pages: export both head/tail info
138138
* they together define a compound page's start/end pos and order
139139
*/
140140
if (PageHead(page))
141-
u |= 1 << KPF_COMPOUND_HEAD;
141+
u |= BIT_ULL(KPF_COMPOUND_HEAD);
142142
if (PageTail(page))
143-
u |= 1 << KPF_COMPOUND_TAIL;
143+
u |= BIT_ULL(KPF_COMPOUND_TAIL);
144144
if (PageHuge(page))
145-
u |= 1 << KPF_HUGE;
145+
u |= BIT_ULL(KPF_HUGE);
146146
/*
147147
* PageTransCompound can be true for non-huge compound pages (slab
148148
* pages or pages allocated by drivers with __GFP_COMP) because it
@@ -153,38 +153,37 @@ u64 stable_page_flags(struct page *page)
153153
struct page *head = compound_head(page);
154154

155155
if (PageLRU(head) || PageAnon(head))
156-
u |= 1 << KPF_THP;
156+
u |= BIT_ULL(KPF_THP);
157157
else if (is_huge_zero_page(head)) {
158-
u |= 1 << KPF_ZERO_PAGE;
159-
u |= 1 << KPF_THP;
158+
u |= BIT_ULL(KPF_ZERO_PAGE);
159+
u |= BIT_ULL(KPF_THP);
160160
}
161161
} else if (is_zero_pfn(page_to_pfn(page)))
162-
u |= 1 << KPF_ZERO_PAGE;
163-
162+
u |= BIT_ULL(KPF_ZERO_PAGE);
164163

165164
/*
166165
* Caveats on high order pages: page->_refcount will only be set
167166
* -1 on the head page; SLUB/SLQB do the same for PG_slab;
168167
* SLOB won't set PG_slab at all on compound pages.
169168
*/
170169
if (PageBuddy(page))
171-
u |= 1 << KPF_BUDDY;
170+
u |= BIT_ULL(KPF_BUDDY);
172171
else if (page_count(page) == 0 && is_free_buddy_page(page))
173-
u |= 1 << KPF_BUDDY;
172+
u |= BIT_ULL(KPF_BUDDY);
174173

175174
if (PageOffline(page))
176-
u |= 1 << KPF_OFFLINE;
175+
u |= BIT_ULL(KPF_OFFLINE);
177176
if (PageTable(page))
178-
u |= 1 << KPF_PGTABLE;
177+
u |= BIT_ULL(KPF_PGTABLE);
179178

180179
if (page_is_idle(page))
181-
u |= 1 << KPF_IDLE;
180+
u |= BIT_ULL(KPF_IDLE);
182181

183182
u |= kpf_copy_bit(k, KPF_LOCKED, PG_locked);
184183

185184
u |= kpf_copy_bit(k, KPF_SLAB, PG_slab);
186185
if (PageTail(page) && PageSlab(compound_head(page)))
187-
u |= 1 << KPF_SLAB;
186+
u |= BIT_ULL(KPF_SLAB);
188187

189188
u |= kpf_copy_bit(k, KPF_ERROR, PG_error);
190189
u |= kpf_copy_bit(k, KPF_DIRTY, PG_dirty);
@@ -197,7 +196,7 @@ u64 stable_page_flags(struct page *page)
197196
u |= kpf_copy_bit(k, KPF_RECLAIM, PG_reclaim);
198197

199198
if (PageSwapCache(page))
200-
u |= 1 << KPF_SWAPCACHE;
199+
u |= BIT_ULL(KPF_SWAPCACHE);
201200
u |= kpf_copy_bit(k, KPF_SWAPBACKED, PG_swapbacked);
202201

203202
u |= kpf_copy_bit(k, KPF_UNEVICTABLE, PG_unevictable);

0 commit comments

Comments
 (0)