-
Notifications
You must be signed in to change notification settings - Fork 133
CodeQL guided high risk (potential) file handle leaks DBio.c #375
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
CodeQL guided high risk (potential) file handle leaks DBio.c #375
Conversation
Guided by CodeQL static code analyser. FileMayNotBeClosed.ql FileMayNeverBeClosed.ql Technically the FILE_LOCKS feature leaks the file handle, but maybe this isn't in a perfectly controlled way (with assurance that at some correct point in the program future, all the fd's are eventually closed)
|
Per my comment on the previous PR: "FWIW there are other locking types and schemes that exist, linux at least supports, that maybe more suited to the design goals trying to be met." ---Yes, could not agree more. |
|
The Merge Status: merge on hold, pending rework expected (review this status in 4 weeks) |
|
I have to recall conversations from around 1992, but I think the problem with having a lock file was that lock files could get left behind if the program crashed, but a file lock handled by the system would be released if the program crashed. The file locking mechanism implemented was the only one (that I am aware of) that was available at the time. Please enumerate the options. |
|
Yes that maybe considered an issue, but the policy at a slightly later time (~1995) was to create the lockfile and write into it the At the time another process that found the lockfile existed, would read it, then could send a This was the basic design that allowed stale lockfile to be spotted and removed, automatically in the 99.99% of cases, due to the owning PID not being alive. Then to enhance this, use of Today the PID number space has expanded somewhat, so less likely to see recycled PID. The benefit of only locking the the Back in 1992 I'm sure lockfiles on NFS systems might have also been an issue but these days even that is not a problem. Visible lockfiles I find are very useful for a human to know, correct/workaround make a decision about. How to handle things when one is found, is a UI matter with appropriate dialogs (popup modals with options to user), or diagnostic command suggestion in the error output such as when in batch mode on how to correct it: In modern times some system will create a symlink like: This way I guess it is possible to check when you |
|
Moved to draft to better indicate current status. |
Should the
cd_fdfield be managed by a newflock_close(int fd)function, where the application can hand the open FD back to flock.c to manage when the real close should occur ? Which in turn, will receive the other active FD when it is also closed (or maybe there is also aflock_fclose(FILE *fp)to manage stdio use cases). The point is flock.c needs visibility of open and closes, then it can manage the locking requirements with multiple users in same process.Then when all uses of a file are close (active_use_count_of_this_fd==0) then flock.c will manage closing all FDs (causing file-locking release) ?
Maybe if the sequence of events can be written in a issue comment, highlight the potential data-race-data-corruption concern (the need for a file lock in the first place) in the order of events. I can better understand how the existing code meets requirements.
In this one function there are multiple
fp = fopen(expandname)thenfclose(fp)that occur, without being locking-aware. Then at the end of the function, there is this lock-aware section, my patch touches. The call to fclose(fp) will destroy any existing locks that may have been obtained before, on that file. The file looks to be appended/write/rewind/truncate etc... and then closed(), if there were to happen more than once the 2nd use of this function the same file, would invalidate the 1st use's locks that were laid down. (due to theclose(fp)syscall getting called from fclose use above)In short you can't mix locking-aware and locking-unaware file open/access to the same file. Either all access to a particular file should be locked/locking-aware, or all accesses to a particular file never obey any locking rules (even when another process is successfully placing locks because it is locking-aware, in this scenario we did not participate in their locking scheme, as it is a co-operative scheme).
FWIW there are other locking types and schemes that exist, linux at least supports, that maybe more suited to the design goals trying to be met.
DBio.c: CodeQL File{MayNot,Never}BeClosed.ql file-handle resource leaks
Guided by CodeQL static code analyser.
FileMayNotBeClosed.ql
FileMayNeverBeClosed.ql
Technically the FILE_LOCKS feature leaks the file handle, but maybe this isn't in a perfectly controlled way (with assurance that at some correct point in the program future, all the fd's are eventually closed)