Re: BUG #16329: Valgrind detects an invalid read when building a gist index with buffering
Pavel Borisov <pashkin.elfe@gmail.com>
From: Pavel Borisov <pashkin.elfe@gmail.com>
To: Tom Lane <tgl@sss.pgh.pa.us>
Cc: Alexander Lakhin <exclusion@gmail.com>, pgsql-bugs@lists.postgresql.org
Date: 2020-11-02T16:55:14Z
Lists: pgsql-bugs
Attachments
- v2-0001-Fix-for-GiST-buffered-build-bug-related-to-contex.patch (application/octet-stream) patch v2-0001
пн, 2 нояб. 2020 г. в 20:05, Tom Lane <tgl@sss.pgh.pa.us>: > Alexander Lakhin <exclusion@gmail.com> writes: > >> Please look at the patch that modifies the gist regression test to make > >> the issue visible and fixes it by avoiding access to the memory context > >> that can be reset in gistProcessEmptyingQueue(). > > > Could you please let me know if the fix is incorrect (or not elaborated > > enough to be included in the upcoming releases) or this issue is false > > positive? > > I took a look at this. I see the hazard, but I'm not sure that > I like your proposed quick-fix. Essentially, the problem is that > gistBuildCallback is supposing that the tuple it creates will survive > the execution of its subroutines, which in fact it won't always. > But that means that at some point the tuple pointer it's passed down to > those subroutines becomes a dangling pointer. What guarantee would we > have that the subroutines don't access the tuple after that point? > > I think the real issue here is confusion about which level of function > "owns" the tempCxt and gets to decide when to reset it. We can't have > both gistBuildCallback and gistProcessEmptyingQueue doing that. Maybe > we need more than one temporary context, or maybe we could just skip > the context reset in gistProcessEmptyingQueue and let the storage > accumulate till we get back to gistBuildCallback. But in any case > it's going to require more analysis. > I investigated the code and realized that there are two ways to call the problematic gistProcessEmptyingQueue() function: 1. gistEmptyAllBuffers /tempCxt owner/ -> gistProcessEmptyingQueue A potentially dangerous operation of resetting memory context inside sub-function calls of a 'context owner' function is done at the very ending of gistProcessEmptyingQueue(). Just after it we return back to gistEmptyAllBuffers and immediately have a switch to old context. So resetting tempCxt has no danger in this case as no pointers are used after. 2. gistBuildCallback /tempCxt owner/ -> gistBufferingBuildInsert -> gistProcessEmptyingQueue gistProcessEmptyingQueue() is at the very ending of gistBufferingBuildInsert(). After the reset no pointers from tempCxt context are used up to the 'owner' level of gistBuildCallback. So moving the only itup pointer operation before gistBufferingBuildInsert() call will fix the bug. (Of course, the alternative of not re-setting tempCxt inside gistProcessEmptyingQueue() and doing this level up will also fix the issue) So I think that the patch by Alexander will do the thing. I also added some comments to the code and removed extra context reset in gistBuildCallback (which is already done level down) to make things clear. -- Best regards, Pavel Borisov Postgres Professional: http://postgrespro.com <http://www.postgrespro.com>
Commits
-
Fix dereference of dangling pointer in GiST index buffering build.
- d2a1d4b190ec 12.15 landed
- b5c6776c1127 11.20 landed
- a1904c9ce56a 14.8 landed
- 8e5eef50c5b4 16.0 landed
- 2dc77adc768e 15.3 landed
- 2adb6adad5f8 13.11 landed
-
Fix GiST buffering build to work when there are included columns.
- 962ab473ec3d 13.1 landed
- 371668a8389d 14.0 landed
- 12945874ebd6 12.5 landed
-
Re-allow testing of GiST buffered builds.
- 78c0b6ed273a 14.0 landed
-
Add support for building GiST index by sorting.
- 16fa9b2b30a3 14.0 cited