Skip to content

Commit

Permalink
Draft of switching namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
michalbiesek committed Jul 22, 2022
1 parent 9100a01 commit 6b467ce
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 5 deletions.
4 changes: 2 additions & 2 deletions cli/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ require (
github.com/ahmetb/go-linq/v3 v3.2.0
github.com/ahmetb/govvv v0.3.0 // indirect
github.com/alicebob/gopher-json v0.0.0-20200520072559-a9ecdc9d1d3a
github.com/c9s/goprocinfo v0.0.0-20210130143923-c95fcf8c64a8
github.com/dlclark/regexp2 v1.4.0 // indirect
github.com/dop251/goja v0.0.0-20201221183957-6b6d5e2b5d80
github.com/fatih/color v1.7.0
Expand All @@ -20,6 +19,7 @@ require (
github.com/huandu/xstrings v1.3.2 // indirect
github.com/imdario/mergo v0.3.11 // indirect
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51
github.com/michalbiesek/goprocinfo v0.0.0-20220722085539-2023181de7cf // indirect
github.com/mitchellh/copystructure v1.1.1 // indirect
github.com/mitchellh/go-ps v1.0.0
github.com/mitchellh/mapstructure v1.4.1
Expand All @@ -31,7 +31,7 @@ require (
github.com/syndtr/gocapability v0.0.0-20200815063812-42c35b437635
github.com/yuin/gopher-lua v0.0.0-20210529063254-f4c35e4016d9 // indirect
golang.org/x/crypto v0.0.0-20201002170205-7f63de1d35b0
golang.org/x/sys v0.0.0-20201223074533-0d417f636930 // indirect
golang.org/x/sys v0.0.0-20220721230656-c6bc011c0c49 // indirect
gopkg.in/yaml.v2 v2.4.0
k8s.io/api v0.20.2
k8s.io/apimachinery v0.20.2
Expand Down
6 changes: 4 additions & 2 deletions cli/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,6 @@ github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+Ce
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs=
github.com/bketelsen/crypt v0.0.3-0.20200106085610-5cbc8cc4026c/go.mod h1:MKsuJmJgSg28kpZDP6UIiPt0e0Oz0kqKNGyRaWEPv84=
github.com/c9s/goprocinfo v0.0.0-20210130143923-c95fcf8c64a8 h1:SjZ2GvvOononHOpK84APFuMvxqsk3tEIaKH/z4Rpu3g=
github.com/c9s/goprocinfo v0.0.0-20210130143923-c95fcf8c64a8/go.mod h1:uEyr4WpAH4hio6LFriaPkL938XnrvLpNPmQHBdrmbIE=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/census-instrumentation/opencensus-proto v0.3.0/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
Expand Down Expand Up @@ -423,6 +421,8 @@ github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m
github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU=
github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/michalbiesek/goprocinfo v0.0.0-20220722085539-2023181de7cf h1:Q6FQkYZMUhRwq8MSk9A8WOe9ngNTEWT749aYqrRJUIE=
github.com/michalbiesek/goprocinfo v0.0.0-20220722085539-2023181de7cf/go.mod h1:qJY6QBaeRXZAwr/BIMIhlhOnmoE4e5yH8MctDULCJJ0=
github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg=
github.com/miekg/dns v1.1.26/go.mod h1:bPDLeHnStXmXAq1m/Ch/hvfNHr14JKNPMBo3VZKjuso=
github.com/miekg/dns v1.1.41/go.mod h1:p6aan82bvRIyn+zDIv9xYNUpwa73JcSh9BKwknJysuI=
Expand Down Expand Up @@ -814,6 +814,8 @@ golang.org/x/sys v0.0.0-20211124211545-fe61309f8881/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20211205182925-97ca703d548d/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9 h1:nhht2DYV/Sn3qOayu8lM+cU1ii9sTLUeBQwQQfUHtrs=
golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220721230656-c6bc011c0c49 h1:TMjZDarEwf621XDryfitp/8awEhiZNiwgphKlTMGRIg=
golang.org/x/sys v0.0.0-20220721230656-c6bc011c0c49/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand Down
11 changes: 11 additions & 0 deletions cli/run/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,17 @@ func (rc *Config) Attach(args []string) error {
if res {
return errors.New("Attach failed. This process is already being scoped")
}

ns_status, pid_ns, err := util.PidNamespace(pid)
if err != nil {
return fmt.Errorf("error reading namespace information: \"%v\"", err)
}
if ns_status {
if err := util.PidSwitchNamespace(pid); err != nil {
return fmt.Errorf("error switching namespace %v", err)
}
args[0] = fmt.Sprint(pid_ns)
}
// Create ldscope
if err := createLdscope(); err != nil {
return fmt.Errorf("error creating ldscope: %v", err)
Expand Down
25 changes: 24 additions & 1 deletion cli/util/proc.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import (
"strconv"
"strings"

linuxproc "github.com/c9s/goprocinfo/linux"
linuxproc "github.com/michalbiesek/goprocinfo/linux"
"golang.org/x/sys/unix"
)

// Process is a unix process
Expand Down Expand Up @@ -139,6 +140,28 @@ func ProcessesScoped() (Processes, error) {
return processes, nil
}

func PidSwitchNamespace(pid int) error {
pidFd, err := unix.PidfdOpen(pid, 0)
if err != nil {
return err
}
return unix.Setns(pidFd, unix.CLONE_NEWUTS|unix.CLONE_NEWPID|unix.CLONE_NEWNS)
}

func PidNamespace(pid int) (bool, int64, error) {
pidStatusPath := fmt.Sprintf("/proc/%v/status", pid)

// Get information about nspid from status
pStat, err := linuxproc.ReadProcessStatus(pidStatusPath)
if err != nil {
return false, -1, fmt.Errorf("error getting nspid: %v", err)
}
if len(pStat.NSpid) > 1 {
return true, pStat.NSpid[1], nil
}
return false, pStat.NSpid[0], nil
}

// PidUser returns the user owning the process specified by PID
func PidUser(pid int) (string, error) {
pidStatusPath := fmt.Sprintf("/proc/%v/status", pid)
Expand Down

0 comments on commit 6b467ce

Please sign in to comment.