登录
首页 >  Golang >  Go问答

禁止在Golang中使用Fork/Exec /bin/sh操作

来源:stackoverflow

时间:2024-02-15 12:45:17 403浏览 收藏

珍惜时间,勤奋学习!今天给大家带来《禁止在Golang中使用Fork/Exec /bin/sh操作》,正文内容主要涉及到等等,如果你正在学习Golang,或者是对Golang有疑问,欢迎大家关注我!后面我会持续更新相关内容的,希望都能帮到正在学习的大家!

问题内容

package main

import (
    "fmt"
    "os"
    "os/exec"
    "syscall"
)

func main() {
    os.Setuid(1000)
    cmd := exec.Command("/bin/sh")

    cmd.Stdin = os.Stdin
    cmd.Stdout = os.Stdout
    cmd.Stderr = os.Stderr

    cmd.Env = []string{"PS1=-[ns-process]- # "}
    cmd.SysProcAttr = &syscall.SysProcAttr{
        Cloneflags: syscall.CLONE_NEWNS |
            syscall.CLONE_NEWUTS |
            syscall.CLONE_NEWIPC |
            syscall.CLONE_NEWPID |
            syscall.CLONE_NEWNET |
            syscall.CLONE_NEWUSER,
        UidMappings: []syscall.SysProcIDMap{
            {
                ContainerID: 0,
                HostID:      os.Getuid(),
                Size:        1,
            },
        },
        GidMappings: []syscall.SysProcIDMap{
            {
                ContainerID: 0,
                HostID:      os.Getgid(),
                Size:        1,
            },
        },
    }

    if err := cmd.Run(); err != nil {
        fmt.Printf("Error running the /bin/sh command - %s\n", err)
        os.Exit(1)
    }
}

上面是我的代码,但是在root用户下运行程序时得到的结果是“fork/exec /bin/sh操作不允许”。但它以uid 1000正确运行。

操作系统:5.15.55-1-manjaro 转到版本:1.18.3


正确答案


参见https://walac.github.io/golang-patch/

文中关于的知识介绍,希望对你的学习有所帮助!若是受益匪浅,那就动动鼠标收藏这篇《禁止在Golang中使用Fork/Exec /bin/sh操作》文章吧,也可关注golang学习网公众号了解相关技术文章。

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>