Skip to content

Commit

Permalink
Make sure mountoptions.{Send, Recv} respects ctx's deadline (#345)
Browse files Browse the repository at this point in the history
`net.{Listen, Dial}` functions does not accept context argument, but
only `{ListenContext, DialContext}` counterparts do. But even those
functions does not respect to context's deadline, so we need to call
`SetDeadline` with passed context's deadline explicitly to ensure
context's deadline is respected.

---

By submitting this pull request, I confirm that you can use, modify,
copy, and redistribute this contribution, under the terms of your
choice.

Signed-off-by: Burak Varlı <[email protected]>
  • Loading branch information
unexge authored Jan 17, 2025
1 parent 2e98bab commit 3613f08
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions pkg/podmounter/mountoptions/mount_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,23 @@ func Send(ctx context.Context, sockPath string, options Options) error {
return fmt.Errorf("failed to marshal message to send %s: %w", sockPath, err)
}

conn, err := net.Dial("unix", sockPath)
var d net.Dialer
conn, err := d.DialContext(ctx, "unix", sockPath)
if err != nil {
return fmt.Errorf("failed to dial to unix socket %s: %w", sockPath, err)
}
defer conn.Close()

unixConn := conn.(*net.UnixConn)

// `unixConn.WriteMsgUnix` does not respect `ctx`'s deadline, we need to call `unixConn.SetDeadline` to ensure `unixConn.WriteMsgUnix` has a deadline.
if deadline, ok := ctx.Deadline(); ok {
err := unixConn.SetDeadline(deadline)
if err != nil {
return fmt.Errorf("failed to set deadline on unix socket %s: %w", sockPath, err)
}
}

unixRights := syscall.UnixRights(options.Fd)
messageN, unixRightsN, err := unixConn.WriteMsgUnix(message, unixRights, nil)
if err != nil {
Expand All @@ -55,20 +64,30 @@ func Send(ctx context.Context, sockPath string, options Options) error {

var (
messageRecvSize = 1024
// We only pass one file descriptor and its 32 bits
// We only pass one file descriptor and it's 32 bits
unixRightsRecvSize = syscall.CmsgSpace(4)
)

// Recv receives passed mount options via `Send` function through given `sockPath`.
func Recv(ctx context.Context, sockPath string) (Options, error) {
warnAboutLongUnixSocketPath(sockPath)

l, err := net.Listen("unix", sockPath)
var lc net.ListenConfig
l, err := lc.Listen(ctx, "unix", sockPath)
if err != nil {
return Options{}, fmt.Errorf("failed to listen unix socket %s: %w", sockPath, err)
}
defer l.Close()

// `l.Accept` does not respect `ctx`'s deadline, we need to call `ul.SetDeadline` to ensure `l.Accept` has a deadline.
if deadline, ok := ctx.Deadline(); ok {
ul := l.(*net.UnixListener)
err := ul.SetDeadline(deadline)
if err != nil {
return Options{}, fmt.Errorf("failed to set deadline on unix socket %s: %w", sockPath, err)
}
}

conn, err := l.Accept()
if err != nil {
return Options{}, fmt.Errorf("failed to accept connection from unix socket %s: %w", sockPath, err)
Expand Down

0 comments on commit 3613f08

Please sign in to comment.