Skip to content

Instantly share code, notes, and snippets.

@estesp
Created April 9, 2015 16:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save estesp/e9d05f695ff2f45cefea to your computer and use it in GitHub Desktop.
Save estesp/e9d05f695ff2f45cefea to your computer and use it in GitHub Desktop.
User namespaces /dev/std{out|err} write access

list /proc/self/fd without allocating pty:

$ docker run testopenfd ls -l /proc/self/fd/
ls: /proc/self/fd/3: cannot read link: No such file or directory
total 0
lr-x------    1 root     root            64 Apr  9 16:14 0 -> /dev/null
l-wx------    1 root     root            64 Apr  9 16:14 1 -> pipe:[621697]
l-wx------    1 root     root            64 Apr  9 16:14 2 -> pipe:[621698]
lr-x------    1 root     root            64 Apr  9 16:14 3

list proc/self/fd with tty turned on:

$ docker run -t testopenfd ls -l /proc/self/fd/
total 0
lrwx------    1 root     root            64 Apr  9 16:15 0 -> /0
lrwx------    1 root     root            64 Apr  9 16:15 1 -> /0
lrwx------    1 root     root            64 Apr  9 16:15 2 -> /0
ls: /proc/self/fd/3: cannot read link: No such file or directory
lr-x------    1 root     root            64 Apr  9 16:15 3

Run openfds when no pty allocated (tries to write to pipe):

$ docker run testopenfd
Error on open: open /proc/self/fd/1: permission denied
Can't write, err = invalid argument

Run openfds when tty turned on:

$ docker run -ti testopenfd
Hello there

openfds.go

package main

import (
	"fmt"
	"os"
)

func main() {

	// open stdout directly as fd 1
	file, err := os.OpenFile("/proc/self/fd/1", os.O_APPEND|os.O_WRONLY, 0660)
	if err != nil {
		fmt.Printf("Error on open: %v\n", err)
	}
	_, err = file.WriteString("Hello there")
	if err != nil {
		fmt.Printf("Can't write, err = %v\n", err)
	}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment