xen/gnttab: Fix TOCTOU race in gnttab_set_version()
PATCH 1/1 xen/gnttab: Fix TOCTOU race in gnttab_set_version()
Move first read of gt->gt_version inside the critical region of the rwlock, otherwise concurrent gnttab operations (silly as they would be) may get mutually confused as to the actual current version.
---
This is far from a problem in practice, because kernels invoke this once and then are done. Still, correctness mandates correctness.
There are a number of lockless reads of gt_version (e.g: right after unlock), but they aren't very worrying because they are effectively snapshots of the instantaneous version. I'd feel better if they were all atomic_read(), but all Xen ports guarantee atomic access on aligned 4 octet fields, so I couldn't be bothered to go chase them.
--- xen/common/grant_table.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
xen/common/grant_table.c +3 -2
@@ -3184,11 +3184,12 @@ gnttab_set_version(XEN_GUEST_HANDLE_PARAM(gnttab_set_version_t) uop)if ( op.version ==2 && gt->max_version ==1 ) goto out;/* Behave as before set_version was introduced. */ +grant_write_lock (gt); + res =0 ;if ( gt->gt_version == op.version ) - goto out; + goto out_unlock; -grant_write_lock (gt);/* * Make sure that the grant table isn't currently in use when we * change the version number, except for the first 8 entries which
base-commit: aaa34f23ac65b75c94d069e407a2698602f18d56
Signature · 1 line
2.43.0
Quoted 7 lines · Alejandro Vallejo
Move first read of
gt->gt_versioninside the critical region of the rwlock, otherwise concurrent gnttab operations (silly as they would be) may get mutually confused as to the actual current version.Fixes c1488502c949("grant-tables: do not fail attempts to...")Reported-by Oleksandr Tyshchenko <[email protected]>Signed-off-by Alejandro Vallejo <[email protected]>
Quoted 5 lines
There are a number of lockless reads of
gt_version(e.g: right after unlock), but they aren't very worrying because they are effectively snapshots of the instantaneous version. I'd feel better if they were allatomic_read(), but all Xen ports guarantee atomic access on aligned 4 octet fields, so I couldn't be bothered to go chase them.
Sooner or later we will want to deal with all (latent) problems of this kind.
Jan
Quoted 10 lines · Jan Beulich
Quoted 7 lines · Alejandro Vallejo
Move first read of
gt->gt_versioninside the critical region of the rwlock, otherwise concurrent gnttab operations (silly as they would be) may get mutually confused as to the actual current version.Fixes c1488502c949("grant-tables: do not fail attempts to...")Reported-by Oleksandr Tyshchenko <[email protected]>Signed-off-by Alejandro Vallejo <[email protected]>Reviewed-by Jan Beulich <[email protected]>
Thanks
Quoted 8 lines
Quoted 6 lines
There are a number of lockless reads of
gt_version(e.g: right after unlock), but they aren't very worrying because they are effectively snapshots of the instantaneous version. I'd feel better if they were allatomic_read(), but all Xen ports guarantee atomic access on aligned 4 octet fields, so I couldn't be bothered to go chase them.Sooner or later we will want to deal with all (latent) problems of this kind.
Quite. Concurrency is hard enough without making assumptions about the underlying ISAs :/
Cheers,
Alejandro