I was told, but never able to confirm it, that Aeroflot carried a gun or guns in a locked box on the flight deck. Not sure if it was to stop hijackers or the crew from defecting to the West.
In todays scenario a locked box on the flight deck, keys with the crew, possibly collected from crew reporting or handed over from crew to crew, would be feasible and no one would have to pass security with a gun in their possession.
SOP would dictate where the weapon was kept during flight, transit etc.