* [PATCH bpf] bpf: Fix offset calculation error in __copy_map_value and zero_map_value
@ 2022-11-11 12:56 Xu Kuohai
2022-11-11 19:17 ` sdf
` (2 more replies)
0 siblings, 3 replies; 6+ messages in thread
From: Xu Kuohai @ 2022-11-11 12:56 UTC (permalink / raw)
To: bpf, linux-kernel
Cc: Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko,
Martin KaFai Lau, Song Liu, Yonghong Song, John Fastabend,
KP Singh, Stanislav Fomichev, Hao Luo, Jiri Olsa,
Kumar Kartikeya Dwivedi
From: Xu Kuohai <xukuohai@huawei.com>
Function __copy_map_value and zero_map_value miscalculated copy offset,
resulting in possible copy of unwanted data to user or kernel.
Fix it.
Fixes: cc48755808c6 ("bpf: Add zero_map_value to zero map value with special fields")
Fixes: 4d7d7f69f4b1 ("bpf: Adapt copy_map_value for multiple offset case")
Signed-off-by: Xu Kuohai <xukuohai@huawei.com>
---
include/linux/bpf.h | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/include/linux/bpf.h b/include/linux/bpf.h
index 74c6f449d81e..c1bd1bd10506 100644
--- a/include/linux/bpf.h
+++ b/include/linux/bpf.h
@@ -315,7 +315,7 @@ static inline void __copy_map_value(struct bpf_map *map, void *dst, void *src, b
u32 next_off = map->off_arr->field_off[i];
memcpy(dst + curr_off, src + curr_off, next_off - curr_off);
- curr_off += map->off_arr->field_sz[i];
+ curr_off = next_off + map->off_arr->field_sz[i];
}
memcpy(dst + curr_off, src + curr_off, map->value_size - curr_off);
}
@@ -344,7 +344,7 @@ static inline void zero_map_value(struct bpf_map *map, void *dst)
u32 next_off = map->off_arr->field_off[i];
memset(dst + curr_off, 0, next_off - curr_off);
- curr_off += map->off_arr->field_sz[i];
+ curr_off = next_off + map->off_arr->field_sz[i];
}
memset(dst + curr_off, 0, map->value_size - curr_off);
}
--
2.30.2
^ permalink raw reply related [flat|nested] 6+ messages in thread* Re: [PATCH bpf] bpf: Fix offset calculation error in __copy_map_value and zero_map_value 2022-11-11 12:56 [PATCH bpf] bpf: Fix offset calculation error in __copy_map_value and zero_map_value Xu Kuohai @ 2022-11-11 19:17 ` sdf 2022-11-11 20:45 ` Kumar Kartikeya Dwivedi 2022-11-11 19:35 ` Kumar Kartikeya Dwivedi 2022-11-11 20:50 ` patchwork-bot+netdevbpf 2 siblings, 1 reply; 6+ messages in thread From: sdf @ 2022-11-11 19:17 UTC (permalink / raw) To: Xu Kuohai Cc: bpf, linux-kernel, Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko, Martin KaFai Lau, Song Liu, Yonghong Song, John Fastabend, KP Singh, Hao Luo, Jiri Olsa, Kumar Kartikeya Dwivedi On 11/11, Xu Kuohai wrote: > From: Xu Kuohai <xukuohai@huawei.com> > Function __copy_map_value and zero_map_value miscalculated copy offset, > resulting in possible copy of unwanted data to user or kernel. > Fix it. > Fixes: cc48755808c6 ("bpf: Add zero_map_value to zero map value with > special fields") > Fixes: 4d7d7f69f4b1 ("bpf: Adapt copy_map_value for multiple offset case") > Signed-off-by: Xu Kuohai <xukuohai@huawei.com> > --- > include/linux/bpf.h | 4 ++-- > 1 file changed, 2 insertions(+), 2 deletions(-) > diff --git a/include/linux/bpf.h b/include/linux/bpf.h > index 74c6f449d81e..c1bd1bd10506 100644 > --- a/include/linux/bpf.h > +++ b/include/linux/bpf.h > @@ -315,7 +315,7 @@ static inline void __copy_map_value(struct bpf_map > *map, void *dst, void *src, b > u32 next_off = map->off_arr->field_off[i]; > memcpy(dst + curr_off, src + curr_off, next_off - curr_off); > - curr_off += map->off_arr->field_sz[i]; > + curr_off = next_off + map->off_arr->field_sz[i]; > } > memcpy(dst + curr_off, src + curr_off, map->value_size - curr_off); > } > @@ -344,7 +344,7 @@ static inline void zero_map_value(struct bpf_map > *map, void *dst) > u32 next_off = map->off_arr->field_off[i]; > memset(dst + curr_off, 0, next_off - curr_off); > - curr_off += map->off_arr->field_sz[i]; > + curr_off = next_off + map->off_arr->field_sz[i]; > } > memset(dst + curr_off, 0, map->value_size - curr_off); > } Hmm, does it mean that it currently works only for the cases where these special fields are first/last? Also, what about bpf-next? The same problem seem to exist there? Might be a good idea to have some selftest to exercise this? > -- > 2.30.2 ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH bpf] bpf: Fix offset calculation error in __copy_map_value and zero_map_value 2022-11-11 19:17 ` sdf @ 2022-11-11 20:45 ` Kumar Kartikeya Dwivedi 2022-11-12 10:25 ` Xu Kuohai 0 siblings, 1 reply; 6+ messages in thread From: Kumar Kartikeya Dwivedi @ 2022-11-11 20:45 UTC (permalink / raw) To: sdf Cc: Xu Kuohai, bpf, linux-kernel, Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko, Martin KaFai Lau, Song Liu, Yonghong Song, John Fastabend, KP Singh, Hao Luo, Jiri Olsa On Sat, Nov 12, 2022 at 12:47:52AM IST, sdf@google.com wrote: > On 11/11, Xu Kuohai wrote: > > From: Xu Kuohai <xukuohai@huawei.com> > > > Function __copy_map_value and zero_map_value miscalculated copy offset, > > resulting in possible copy of unwanted data to user or kernel. > > > Fix it. > > > Fixes: cc48755808c6 ("bpf: Add zero_map_value to zero map value with > > special fields") > > Fixes: 4d7d7f69f4b1 ("bpf: Adapt copy_map_value for multiple offset case") > > Signed-off-by: Xu Kuohai <xukuohai@huawei.com> > > --- > > include/linux/bpf.h | 4 ++-- > > 1 file changed, 2 insertions(+), 2 deletions(-) > > > diff --git a/include/linux/bpf.h b/include/linux/bpf.h > > index 74c6f449d81e..c1bd1bd10506 100644 > > --- a/include/linux/bpf.h > > +++ b/include/linux/bpf.h > > @@ -315,7 +315,7 @@ static inline void __copy_map_value(struct bpf_map > > *map, void *dst, void *src, b > > u32 next_off = map->off_arr->field_off[i]; > > > memcpy(dst + curr_off, src + curr_off, next_off - curr_off); > > - curr_off += map->off_arr->field_sz[i]; > > + curr_off = next_off + map->off_arr->field_sz[i]; > > } > > memcpy(dst + curr_off, src + curr_off, map->value_size - curr_off); > > } > > @@ -344,7 +344,7 @@ static inline void zero_map_value(struct bpf_map > > *map, void *dst) > > u32 next_off = map->off_arr->field_off[i]; > > > memset(dst + curr_off, 0, next_off - curr_off); > > - curr_off += map->off_arr->field_sz[i]; > > + curr_off = next_off + map->off_arr->field_sz[i]; > > } > > memset(dst + curr_off, 0, map->value_size - curr_off); > > } > > Hmm, does it mean that it currently works only for the cases where > these special fields are first/last? > > Also, what about bpf-next? The same problem seem to exist there? > Replied with the patch in the other email. > Might be a good idea to have some selftest to exercise this? > I agree, there was another bug in the same code before this, so I think we should add tests for this (I should have done that with the commit being fixed...). Xu, if you have cycles, can you work on testing a few edge cases and make sure we don't regress in the future? Otherwise I will take a look next week. ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH bpf] bpf: Fix offset calculation error in __copy_map_value and zero_map_value 2022-11-11 20:45 ` Kumar Kartikeya Dwivedi @ 2022-11-12 10:25 ` Xu Kuohai 0 siblings, 0 replies; 6+ messages in thread From: Xu Kuohai @ 2022-11-12 10:25 UTC (permalink / raw) To: Kumar Kartikeya Dwivedi, sdf Cc: Xu Kuohai, bpf, linux-kernel, Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko, Martin KaFai Lau, Song Liu, Yonghong Song, John Fastabend, KP Singh, Hao Luo, Jiri Olsa On 11/12/2022 4:45 AM, Kumar Kartikeya Dwivedi wrote: > On Sat, Nov 12, 2022 at 12:47:52AM IST, sdf@google.com wrote: >> On 11/11, Xu Kuohai wrote: >>> From: Xu Kuohai <xukuohai@huawei.com> >> >>> Function __copy_map_value and zero_map_value miscalculated copy offset, >>> resulting in possible copy of unwanted data to user or kernel. >> >>> Fix it. >> >>> Fixes: cc48755808c6 ("bpf: Add zero_map_value to zero map value with >>> special fields") >>> Fixes: 4d7d7f69f4b1 ("bpf: Adapt copy_map_value for multiple offset case") >>> Signed-off-by: Xu Kuohai <xukuohai@huawei.com> >>> --- >>> include/linux/bpf.h | 4 ++-- >>> 1 file changed, 2 insertions(+), 2 deletions(-) >> >>> diff --git a/include/linux/bpf.h b/include/linux/bpf.h >>> index 74c6f449d81e..c1bd1bd10506 100644 >>> --- a/include/linux/bpf.h >>> +++ b/include/linux/bpf.h >>> @@ -315,7 +315,7 @@ static inline void __copy_map_value(struct bpf_map >>> *map, void *dst, void *src, b >>> u32 next_off = map->off_arr->field_off[i]; >> >>> memcpy(dst + curr_off, src + curr_off, next_off - curr_off); >>> - curr_off += map->off_arr->field_sz[i]; >>> + curr_off = next_off + map->off_arr->field_sz[i]; >>> } >>> memcpy(dst + curr_off, src + curr_off, map->value_size - curr_off); >>> } >>> @@ -344,7 +344,7 @@ static inline void zero_map_value(struct bpf_map >>> *map, void *dst) >>> u32 next_off = map->off_arr->field_off[i]; >> >>> memset(dst + curr_off, 0, next_off - curr_off); >>> - curr_off += map->off_arr->field_sz[i]; >>> + curr_off = next_off + map->off_arr->field_sz[i]; >>> } >>> memset(dst + curr_off, 0, map->value_size - curr_off); >>> } >> >> Hmm, does it mean that it currently works only for the cases where >> these special fields are first/last? >> >> Also, what about bpf-next? The same problem seem to exist there? >> > > Replied with the patch in the other email. > >> Might be a good idea to have some selftest to exercise this? >> > > I agree, there was another bug in the same code before this, so I think we > should add tests for this (I should have done that with the commit being > fixed...). > > Xu, if you have cycles, can you work on testing a few edge cases and make sure > we don't regress in the future? Otherwise I will take a look next week. > . Ok, I'll add a few cases to test_sk_storage_map to capture this ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH bpf] bpf: Fix offset calculation error in __copy_map_value and zero_map_value 2022-11-11 12:56 [PATCH bpf] bpf: Fix offset calculation error in __copy_map_value and zero_map_value Xu Kuohai 2022-11-11 19:17 ` sdf @ 2022-11-11 19:35 ` Kumar Kartikeya Dwivedi 2022-11-11 20:50 ` patchwork-bot+netdevbpf 2 siblings, 0 replies; 6+ messages in thread From: Kumar Kartikeya Dwivedi @ 2022-11-11 19:35 UTC (permalink / raw) To: Xu Kuohai Cc: bpf, linux-kernel, Alexei Starovoitov, Daniel Borkmann, Andrii Nakryiko, Martin KaFai Lau, Song Liu, Yonghong Song, John Fastabend, KP Singh, Stanislav Fomichev, Hao Luo, Jiri Olsa On Fri, Nov 11, 2022 at 06:26:20PM IST, Xu Kuohai wrote: > From: Xu Kuohai <xukuohai@huawei.com> > > Function __copy_map_value and zero_map_value miscalculated copy offset, > resulting in possible copy of unwanted data to user or kernel. > > Fix it. > > Fixes: cc48755808c6 ("bpf: Add zero_map_value to zero map value with special fields") > Fixes: 4d7d7f69f4b1 ("bpf: Adapt copy_map_value for multiple offset case") > Signed-off-by: Xu Kuohai <xukuohai@huawei.com> > --- Acked-by: Kumar Kartikeya Dwivedi <memxor@gmail.com> I also have a fix here for bpf-next, since this won't apply to it: https://lore.kernel.org/bpf/20221111193224.876706-4-memxor@gmail.com I think it'd be best if this one gets applied to bpf and mine to bpf-next, and any conflicts are resolved when merging both trees (the conflict is trivial), but I'll leave it up to the maintainers to decide. ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH bpf] bpf: Fix offset calculation error in __copy_map_value and zero_map_value 2022-11-11 12:56 [PATCH bpf] bpf: Fix offset calculation error in __copy_map_value and zero_map_value Xu Kuohai 2022-11-11 19:17 ` sdf 2022-11-11 19:35 ` Kumar Kartikeya Dwivedi @ 2022-11-11 20:50 ` patchwork-bot+netdevbpf 2 siblings, 0 replies; 6+ messages in thread From: patchwork-bot+netdevbpf @ 2022-11-11 20:50 UTC (permalink / raw) To: Xu Kuohai Cc: bpf, linux-kernel, ast, daniel, andrii, martin.lau, song, yhs, john.fastabend, kpsingh, sdf, haoluo, jolsa, memxor Hello: This patch was applied to bpf/bpf.git (master) by Andrii Nakryiko <andrii@kernel.org>: On Fri, 11 Nov 2022 07:56:20 -0500 you wrote: > From: Xu Kuohai <xukuohai@huawei.com> > > Function __copy_map_value and zero_map_value miscalculated copy offset, > resulting in possible copy of unwanted data to user or kernel. > > Fix it. > > [...] Here is the summary with links: - [bpf] bpf: Fix offset calculation error in __copy_map_value and zero_map_value https://git.kernel.org/bpf/bpf/c/1f6e04a1c7b8 You are awesome, thank you! -- Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html ^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2022-11-12 10:25 UTC | newest] Thread overview: 6+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2022-11-11 12:56 [PATCH bpf] bpf: Fix offset calculation error in __copy_map_value and zero_map_value Xu Kuohai 2022-11-11 19:17 ` sdf 2022-11-11 20:45 ` Kumar Kartikeya Dwivedi 2022-11-12 10:25 ` Xu Kuohai 2022-11-11 19:35 ` Kumar Kartikeya Dwivedi 2022-11-11 20:50 ` patchwork-bot+netdevbpf
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox