Skip to content
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

Improve shared memory protection #1464

Merged
merged 6 commits into from
Nov 6, 2022
Merged

Improve shared memory protection #1464

merged 6 commits into from
Nov 6, 2022

Conversation

DL6ER
Copy link
Member

@DL6ER DL6ER commented Nov 3, 2022

By submitting this pull request, I confirm the following:

  • I have read and understood the contributors guide.
  • I have checked that another pull request for this purpose does not exist.
  • I have considered, and confirmed that this submission will be valuable to others.
  • I accept that this submission may not be used, and the pull request closed at the will of the maintainer.
  • I give this submission freely, and claim no ownership to its content.

How familiar are you with the codebase?:

10


Store and check shared memory ownership before resizing shared memory objects.

The scenario we are resolving here is the following: FTL is already running and someone(/-thing) deletes all the shared memory objects. If another instance of FTL is then started, it doesn't know a previous process is running and creates new SHM objects for itself. Both processes can - in theory - run just fine without touching each other as they are both actually using different shared memory objects. You are right when you think this is bizarre. The reason is that the first one has the shared memory objects still mapped into memory, i.e., "deleting" them from disk only removes the visible file handles. The next FTL instance creates files with the very same name, however, they are also new and distinct files, i.e., their memory will point elsewhere. Both instances can now run in parallel just fine until one of them needs to resize a shared memory objects. If one of the shared memory events now gets resized to a size larger than it was before BUT smaller than what the other FTL instance is expecting, the other instance will instantaneously crash with a SIGSEGV (a bus error to be precise).

This PR resolves this by storing the PID of the SHM object creator in the settings object.

Each FTL instance reloads this shared memory instance now before performing any potentially dangerous operation and checks if the shared memory files on disk are still owned by this process. If this is not the case, we are in serious trouble and exit immediately. This should allow the second instance (you could call it the "rightful owner" of the current existing SHM objects) a fairly good chance to never even notice this and continue to operate just fine.

Resolves the root causes behind #1463, #1448 and possibly other similar issues in the past by preventing a certain "mis"configuration to have the bad effect it had for these users.

…aware of a possible DEBUG_SHMEM flag when creating the shared memory segments

Signed-off-by: DL6ER <[email protected]>
… objects. The scenario we are resolving here is the following: FTL is already running and someone(/-thing) deletes all the shared memory objects. If another instance of FTL is then started, it doesn't know a previous process is running and creates new SHM objects for itself. Both processes can - in theory - run just fine without touching each other as they are both actually using *different* shared memory objects. You are right when you think this is bizarre. The reason is that the first one has the shared memory objects still mapped into memory, i.e., "deleting" them from disk only removes the visible file handles. The next FTL instance creates files with the very same name, however, they are also new and distict files, i.e., their memory will point elsewhere. Both instances can now run in parallel just fine *until* one of them needs to resize a shared memory objects. If one of the shared memory events now gets resized to a size larger than it was before BUT smaller than what the other FTL instance is expecting, the other instance will instantaneously crash with a SIGSEGV (Bus error).

This commit resolves this by storing the PID of the SHM object creator in the settings object.
Each FTL instance reloads this shared memory instance now before performing any potentially dangerous operation and checks if the shared memory files on disk are still owned by this process. If this is not the case, we are in serious trouble and exit immediately.
This should allow the second instance (you could call it the "rightful owner" of the current existing SHM objects) a fairly good chance to never even notice this and continue to operate just fine.

Signed-off-by: DL6ER <[email protected]>
…TERM) when trying to restart FTL really early

Signed-off-by: DL6ER <[email protected]>
…imes. The PID in this file will later be overwritten after forking

Signed-off-by: DL6ER <[email protected]>
Signed-off-by: DL6ER <[email protected]>
src/main.c Show resolved Hide resolved
@DL6ER DL6ER merged commit 9f156f5 into development Nov 6, 2022
@DL6ER DL6ER deleted the tweak/shmem branch November 6, 2022 12:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

FTL crashes after system reboot FTL crashed after enabling/disabling a group of blocked domains
2 participants