KubeOS/0003-KubeOS-fix-the-hostshell-cannot-obtain-the-lib.patch
Yuhang Wei 1d36b74685 KubeOS:sync code from source master branch
Signed-off-by: Yuhang Wei <weiyuhang3@huawei.com>
2024-02-26 09:54:27 +08:00

76 lines
2.5 KiB
Diff

From 470e190db2de92b65e7fa720864823e7245c51e9 Mon Sep 17 00:00:00 2001
From: liyuanr <liyuanrong1@huawei.com>
Date: Mon, 7 Aug 2023 19:09:18 +0800
Subject: [PATCH 03/17] KubeOS: fix the hostshell cannot obtain the lib
Fix the hostshell cannot obtain the lib
Signed-off-by: liyuanr <liyuanrong1@huawei.com>
---
cmd/admin-container/main.go | 40 +++++++++++++++++++++++++------------
1 file changed, 27 insertions(+), 13 deletions(-)
diff --git a/cmd/admin-container/main.go b/cmd/admin-container/main.go
index f6a7293..5fa0838 100644
--- a/cmd/admin-container/main.go
+++ b/cmd/admin-container/main.go
@@ -22,28 +22,42 @@ import (
"github.com/sirupsen/logrus"
)
+const (
+ bashPath = "/usr/bin/bash"
+ usrBin = "/usr/bin"
+ usrSbin = "/usr/sbin"
+ localBin = "/usr/local/bin"
+ localSbin = "/usr/local/sbin"
+ usrLib = "/usr/lib"
+ usrLib64 = "/usr/lib64"
+ lib = "/lib"
+ lib64 = "/lib64"
+ envPathPrefix = "PATH=$PATH:"
+ envLdLibrarPathPrefix = "LD_LIBRARY_PATH=$LD_LIBRARY_PATH:"
+)
+
func main() {
EUID := os.Geteuid()
rootEUID := 0 // 0 indicates that the process has the permission of the root user.
if EUID != rootEUID {
logrus.Error("please use root to run hostshell")
- return
+
}
PPID := os.Getppid()
rootFsPath := "/proc/" + strconv.Itoa(PPID) + "/root"
- bashPath := "/usr/bin/bash"
- usrBin := "/usr/bin"
- usrSbin := "/usr/sbin"
- localBin := "/usr/local/bin"
- localSbin := "/usr/local/sbin"
- paths := []string{usrBin, usrSbin, localBin, localSbin}
- for i, p := range paths {
- paths[i] = rootFsPath + p
- }
- path := "PATH=$PATH:" + strings.Join(paths, ":")
- lib := "LD_LIBRARY_PATH=/lib:/lib64:/usr/lib:/usr/lib64:$LD_LIBRARY_PATH"
+ path := concatenateEnvPath(rootFsPath, envPathPrefix, []string{usrBin, usrSbin, localBin, localSbin})
+ libPath := concatenateEnvPath(rootFsPath, envLdLibrarPathPrefix, []string{usrLib, usrLib64, lib, lib64})
if err := syscall.Exec("/usr/bin/nsenter", []string{"nsenter", "-t", "1", "-a",
- "env", "-i", path, lib, rootFsPath + bashPath}, os.Environ()); err != nil {
+ "env", "-i", path, libPath, rootFsPath + bashPath}, os.Environ()); err != nil {
logrus.Error("nsenter excute error", err)
}
}
+
+func concatenateEnvPath(prefix string, envVarPrefix string, paths []string) string {
+ for i, p := range paths {
+ paths[i] = prefix + p
+ }
+ pathLine := envVarPrefix + strings.Join(paths, ":")
+ pathEnv := os.ExpandEnv(pathLine)
+ return pathEnv
+}
--
2.39.0