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

Heap buffer overflow in version 2.8.0.0 with abstract Linux sockets #382

Closed
remyoudompheng opened this issue Feb 18, 2019 · 3 comments
Closed

Comments

@remyoudompheng
Copy link

Hello,

There is a heap overflow in pokeSockAddr that was fixed in release 3.0.0.0 when using Linux abstract Unix sockets. As far as I know it is present in at least all versions from 2.6.3.3 to 2.8.0.0.

The following program reproduces a segfault with ghc 8.6.3 when compiled with the threaded runtime

import Network.Socket
import Control.Monad (forM)

main :: IO ()
main = do
    forM [1..100] $ \n -> do
        putStrLn (show n ++ " characters")
        sock <- socket AF_UNIX Stream defaultProtocol
        bind sock (SockAddrUnix ("\0" ++ (replicate n 't')))
        close sock
    return ()

with the following output:

$ ghc -threaded -dynamic -package network test.hs
$ ./test
...
86 characters
zsh: segmentation fault (core dumped)  ./test

Since many systems are still using network 2.8.0.0 (like the Stackage distribution) I would like to know whether a 2.8.0.1 version could be released.

The fix would be to have pokeSockAddr _ sa@(SockAddrUnix _) always use

zeroMemory p $ fromIntegral $ sizeOfSockAddr sa

like it does in the 3.x version.

remyoudompheng pushed a commit to remyoudompheng/network that referenced this issue Feb 19, 2019
remyoudompheng pushed a commit to remyoudompheng/network that referenced this issue Feb 19, 2019
@eborden
Copy link
Collaborator

eborden commented Feb 19, 2019

@remyoudompheng I would gladly accept a PR for 2.8.0.1.

remyoudompheng pushed a commit to remyoudompheng/network that referenced this issue Feb 19, 2019
There are two cases where pokeSockAddr can write past the
end of the sockaddr_un structure and corrupt the heap:
- when processing a very large Unix socket path (more than 108 bytes)
- when processing the address of a Linux abstract Unix socket,
  where (2 + length path) bytes are allocated but sizeof(sockadr_un)
  are written.
@remyoudompheng
Copy link
Author

For completeness I included in the PR a variant of commit c8042c7 to check lengths of ordinary Unix socket addresses.

You can obtain consistent crashes with a program like

import Network.Socket
import Control.Monad (forM)
import Control.Exception
import System.Directory

main :: IO ()
main = do
    forM [200..1000] $ \n -> do
        putStrLn (show n ++ " characters")
        sock <- socket AF_UNIX Stream defaultProtocol
        do {
            bind sock (SockAddrUnix (replicate n 't' :: String));
            removeFile (replicate n 't' :: FilePath);
        } `catch` (print :: SomeException -> IO ())
        close sock
    return ()

In this case it is probably easier to obtain more arbitrary memory writes.

remyoudompheng pushed a commit to remyoudompheng/network that referenced this issue Feb 23, 2019
There are two cases where pokeSockAddr can write past the
end of the sockaddr_un structure and corrupt the heap:
- when processing a very large Unix socket path
- when processing the address of a Linux abstract Unix socket,
  where (2 + length path) bytes are allocated but sizeof(sockadr_un)
  are written.

The fix is backported from version 3.0.0.0.
kazu-yamamoto pushed a commit to kazu-yamamoto/network that referenced this issue Apr 23, 2019
There are two cases where pokeSockAddr can write past the
end of the sockaddr_un structure and corrupt the heap:
- when processing a very large Unix socket path
- when processing the address of a Linux abstract Unix socket,
  where (2 + length path) bytes are allocated but sizeof(sockadr_un)
  are written.

The fix is backported from version 3.0.0.0.
kazu-yamamoto pushed a commit to kazu-yamamoto/network that referenced this issue Apr 23, 2019
There are two cases where pokeSockAddr can write past the
end of the sockaddr_un structure and corrupt the heap:
- when processing a very large Unix socket path
- when processing the address of a Linux abstract Unix socket,
  where (2 + length path) bytes are allocated but sizeof(sockadr_un)
  are written.

The fix is backported from version 3.0.0.0.
kazu-yamamoto pushed a commit to kazu-yamamoto/network that referenced this issue Apr 23, 2019
There are two cases where pokeSockAddr can write past the
end of the sockaddr_un structure and corrupt the heap:
- when processing a very large Unix socket path
- when processing the address of a Linux abstract Unix socket,
  where (2 + length path) bytes are allocated but sizeof(sockadr_un)
  are written.

The fix is backported from version 3.0.0.0.
kazu-yamamoto pushed a commit to kazu-yamamoto/network that referenced this issue Apr 23, 2019
There are two cases where pokeSockAddr can write past the
end of the sockaddr_un structure and corrupt the heap:
- when processing a very large Unix socket path
- when processing the address of a Linux abstract Unix socket,
  where (2 + length path) bytes are allocated but sizeof(sockadr_un)
  are written.

The fix is backported from version 3.0.0.0.
kazu-yamamoto pushed a commit to kazu-yamamoto/network that referenced this issue Apr 23, 2019
There are two cases where pokeSockAddr can write past the
end of the sockaddr_un structure and corrupt the heap:
- when processing a very large Unix socket path
- when processing the address of a Linux abstract Unix socket,
  where (2 + length path) bytes are allocated but sizeof(sockadr_un)
  are written.

The fix is backported from version 3.0.0.0.
eborden pushed a commit that referenced this issue Apr 24, 2019
There are two cases where pokeSockAddr can write past the
end of the sockaddr_un structure and corrupt the heap:
- when processing a very large Unix socket path
- when processing the address of a Linux abstract Unix socket,
  where (2 + length path) bytes are allocated but sizeof(sockadr_un)
  are written.

The fix is backported from version 3.0.0.0.
@kazu-yamamoto
Copy link
Collaborator

#400 fixes this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants