Skip to content

Commit 940c3b8

Browse files
committed
Fix panic 'provided file is not a console'
As outlined in #3433, containerd/console will panic on a call to console.Current(). This patch provides a simple consoleutil wrapper that will return an error instead. Note that part of #3300 is being reverted, as no longer necessary. This patch does not try to be "smart" and does not check the status of stdin/out/err otherwise or if it is consistent with user provided flags, but merely ensures we do not crash. Signed-off-by: apostasie <spam_blackhole@farcloser.world>
1 parent 2a388a7 commit 940c3b8

File tree

6 files changed

+35
-14
lines changed

6 files changed

+35
-14
lines changed

cmd/nerdctl/container/container_exec.go

-9
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@ package container
1818

1919
import (
2020
"errors"
21-
"os"
2221

23-
"github.com/moby/term"
2422
"github.com/spf13/cobra"
2523

2624
containerd "github.com/containerd/containerd/v2/client"
@@ -88,13 +86,6 @@ func processExecCommandOptions(cmd *cobra.Command) (types.ContainerExecOptions,
8886
}
8987
}
9088

91-
_, isTerminal := term.GetFdInfo(os.Stdin)
92-
if !flagD {
93-
if flagT && flagI && !isTerminal {
94-
return types.ContainerExecOptions{}, errors.New("the input device is not a TTY")
95-
}
96-
}
97-
9889
workdir, err := cmd.Flags().GetString("workdir")
9990
if err != nil {
10091
return types.ContainerExecOptions{}, err

cmd/nerdctl/container/container_run.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,10 @@ func runAction(cmd *cobra.Command, args []string) error {
389389

390390
var con console.Console
391391
if createOpt.TTY && !createOpt.Detach {
392-
con = console.Current()
392+
con, err = consoleutil.Current()
393+
if err != nil {
394+
return err
395+
}
393396
defer con.Reset()
394397
if err := con.SetRaw(); err != nil {
395398
return err

pkg/cmd/container/attach.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,10 @@ func Attach(ctx context.Context, client *containerd.Client, req string, options
6666
con console.Console
6767
)
6868
if spec.Process.Terminal {
69-
con = console.Current()
69+
con, err = consoleutil.Current()
70+
if err != nil {
71+
return err
72+
}
7073
defer con.Reset()
7174
if err := con.SetRaw(); err != nil {
7275
return fmt.Errorf("failed to set the console to raw mode: %w", err)

pkg/cmd/container/exec.go

+9-2
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,10 @@ func execActionWithContainer(ctx context.Context, client *containerd.Client, con
106106

107107
var con console.Console
108108
if options.TTY {
109-
con = console.Current()
109+
con, err = consoleutil.Current()
110+
if err != nil {
111+
return err
112+
}
110113
defer con.Reset()
111114
if err := con.SetRaw(); err != nil {
112115
return err
@@ -164,7 +167,11 @@ func generateExecProcessSpec(ctx context.Context, client *containerd.Client, con
164167
pspec := spec.Process
165168
pspec.Terminal = options.TTY
166169
if pspec.Terminal {
167-
if size, err := console.Current().Size(); err == nil {
170+
con, err := consoleutil.Current()
171+
if err != nil {
172+
return nil, err
173+
}
174+
if size, err := con.Size(); err == nil {
168175
pspec.ConsoleSize = &specs.Box{Height: uint(size.Height), Width: uint(size.Width)}
169176
}
170177
}

pkg/consoleutil/consoleutil.go

+14
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,22 @@ package consoleutil
1818

1919
import (
2020
"context"
21+
"os"
22+
23+
"github.com/containerd/console"
2124
)
2225

26+
// Current is from https://github.com/containerd/console/blob/main/console.go#L68-L80
27+
// adapted so that it does not panic
28+
func Current() (c console.Console, err error) {
29+
for _, s := range []*os.File{os.Stderr, os.Stdout, os.Stdin} {
30+
if c, err = console.ConsoleFromFile(s); err == nil {
31+
return c, nil
32+
}
33+
}
34+
return nil, console.ErrNotAConsole
35+
}
36+
2337
// resizer is from https://github.com/containerd/containerd/blob/v1.7.0-rc.2/cmd/ctr/commands/tasks/tasks.go#L25-L27
2438
type resizer interface {
2539
Resize(ctx context.Context, w, h uint32) error

pkg/containerutil/containerutil.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,10 @@ func Start(ctx context.Context, container containerd.Container, flagA bool, clie
244244
flagT := process.Process.Terminal
245245
var con console.Console
246246
if flagA && flagT {
247-
con = console.Current()
247+
con, err = consoleutil.Current()
248+
if err != nil {
249+
return err
250+
}
248251
defer con.Reset()
249252
if err := con.SetRaw(); err != nil {
250253
return err

0 commit comments

Comments
 (0)