-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
When running with the threaded RTS, the test currently fails, which is expected: we can't support this RTS for now, see #9. See: #9
- Loading branch information
Showing
4 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
module ThreadedScenario (scenario) where | ||
|
||
import Control.Concurrent.Async (Async, wait) | ||
import Control.Concurrent.MVar (newEmptyMVar, takeMVar, putMVar) | ||
import Control.Exception.Base (handleJust) | ||
import System.IO (IOMode(ReadMode), withFile) | ||
import System.IO.Error (isPermissionError) | ||
import System.Posix.Types (CPid(..)) | ||
|
||
import Test.Tasty (TestTree) | ||
import Test.Tasty.HUnit (assertFailure, testCaseSteps) | ||
|
||
import System.Landlock (AccessFsFlag(..), RulesetAttr(..), landlock) | ||
|
||
foreign import ccall unsafe "unistd.h gettid" | ||
gettid :: IO CPid | ||
|
||
scenario :: (IO () -> (Async () -> IO ()) -> IO ()) -> TestTree | ||
scenario fn = testCaseSteps "Multithreaded Scenario" (scenario' fn) | ||
|
||
scenario' :: (IO () -> (Async () -> IO ()) -> IO ()) -> (String -> IO ()) -> IO () | ||
scenario' withAsync step = do | ||
step "Starting scenario" | ||
|
||
mainTid <- gettid | ||
step $ "Main TID = " ++ show mainTid | ||
|
||
tidMVar <- newEmptyMVar | ||
continueMVar <- newEmptyMVar | ||
|
||
step "Launching thread" | ||
withAsync (thread tidMVar continueMVar) $ \child -> do | ||
_ <- takeMVar tidMVar | ||
|
||
step "Setting up Landlock sandbox" | ||
let flags = [AccessFsReadFile] | ||
landlock (RulesetAttr flags) [] [] $ \_ -> return () | ||
|
||
step "Assert file not readable from main thread" | ||
assertFileNotReadable "main" | ||
step "Success" | ||
|
||
step "Letting thread continue" | ||
putMVar continueMVar () | ||
|
||
step "Waiting for thread to exit" | ||
wait child | ||
|
||
where | ||
thread tidMVar continueMVar = do | ||
step "Running in thread" | ||
|
||
tid <- gettid | ||
step $ "Thread TID = " ++ show tid | ||
putMVar tidMVar tid | ||
|
||
step "Waiting for the signal..." | ||
() <- takeMVar continueMVar | ||
step "Received signal, continuing" | ||
|
||
step "Assert file not readable from thread" | ||
assertFileNotReadable "thread" | ||
step "Success" | ||
|
||
file = "/etc/resolv.conf" | ||
assertFileNotReadable env = handleJust permissionError return $ withFile file ReadMode $ \_ -> | ||
assertFailure $ "Still able to open file " ++ file ++ " in " ++ env | ||
permissionError exc = if isPermissionError exc then Just () else Nothing |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
module Main (main) where | ||
|
||
import Control.Concurrent.Async (withAsyncBound) | ||
|
||
import Test.Tasty (defaultMain) | ||
import Test.Tasty.ExpectedFailure (expectFailBecause, ignoreTestBecause) | ||
|
||
import System.Landlock (isSupported) | ||
|
||
import ThreadedScenario (scenario) | ||
|
||
main :: IO () | ||
main = do | ||
supported <- isSupported | ||
let test = scenario withAsyncBound | ||
defaultMain $ | ||
if supported | ||
then expectFailBecause "landlock_restrict_self is thread-bound" test | ||
else ignoreTestBecause "Landlock not supported" test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters