RE: [PATCH] Fix the race between the fget() and close()
From: Liu, Chuansheng <hidden>
Date: 2013-08-31 07:44:35
Also in:
lkml
-----Original Message----- From: Al Viro [mailto:viro@ftp.linux.org.uk] On Behalf Of Al Viro Sent: Saturday, August 31, 2013 3:36 PM To: Liu, Chuansheng Cc: Eric Dumazet; linux-fsdevel@vger.kernel.org; linux-kernel@vger.kernel.org Subject: Re: [PATCH] Fix the race between the fget() and close() On Sat, Aug 31, 2013 at 07:01:33AM +0000, Liu, Chuansheng wrote:quoted
My scenario is: P1 files_struct refcount is 1, P2's is 1 also. P1 get_files_struct(P2) P1 install one file into P2's files_struct P1 put_files_struct(P2) Then P1 and P2's files_struct refcount are 1, then when P1 is doing ioctl() andP2 is exitingquoted
with put_files_struct(P2), the race will occur, my understanding is wrong?First of all, this wouldn't have been a problem (so you get a new reference to file inserted in P2's files_struct; file refcount had been bumped, so destruction of P2's files_struct will undo that increment of file refcount and we are still fine). _Removal_ in a similar scenario would have been a problem, with P2 doing fdget() while its table isn't shared, then P1 removing a reference from it and dropping a file - the last one, at that, since fdget() assumed that the reference would've stayed in P2's descriptor table. HOWEVER, P1 does not do get_files_struct(P2) at all - it's only done by P2 in binder_mmap().
Got it, thanks. In other process, the fget() + _fd_install() should be the same as the process call open() directly, and the file reference count will be at least 2.
Again, the invariant to look for is this: * if current->files had not been shared at fdget() time, it won't be shared at matching fdput() and no entries will have been removed in between. task_fd_install()/task_close_fd() are done on proc->files, which contributes to descriptor table refcount. All other modifications are done to current->files, which also contributes to refcount. If at fdget() time current->files had refcount 1, we had no other processes with task->files pointing to this descriptor table *and* no binder_proc had their ->files pointint to it. No new ones may appear, since new process could get such a reference only from do_fork() called by us and new binder_proc could get such a reference only from binder_mmap() called by us. Neither is called between fdget() and fdput(). So in that case the only reference to this descriptor table will remain current->files and all removals would have to be done by ourselves (and not via task_close_fd(), at that). And AFAICS, binder_lock() prevents proc->files being dropped under task_close_fd() and task_fd_install(). Hell knows... How reproducible it is? Do you have any more instances, or had that been a one-off panic?
Just meet once yet.