relabel.go 3.7 KB
Newer Older
O
overweight 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
// Copyright (c) Huawei Technologies Co., Ltd. 2018-2019. All rights reserved.
// isulad-tools is licensed under the Mulan PSL v1.
// You can use this software according to the terms and conditions of the Mulan PSL v1.
// You may obtain a copy of Mulan PSL v1 at:
//    http://license.coscl.org.cn/MulanPSL
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
// PURPOSE.
// See the Mulan PSL v1 for more details.
// Description: selinux relabel commands
// Author: zhangwei
// Create: 2018-01-18

// go base main package
package main

import (
	"fmt"
	"io/ioutil"
	"os/exec"

	"isula.org/isulad-tools/utils"

	"github.com/opencontainers/runc/libcontainer/selinux"
	"github.com/sirupsen/logrus"
	"github.com/urfave/cli"
)

var (
	// Seconfig is absolute path for SELinux config file
	Seconfig    = "/etc/selinux/config"
	hostSystemd = "/lib/systemd/systemd"
)

type selinuxCommand struct {
	cmd  string
	argv []string
}

type selinuxContext struct {
	conType string
	path    string
}

func relabelIsuladBinary(path, bin string) error {
	cmd := exec.Command("semanage", []string{"fcontext", "-a", "-t", "init_exec_t", fmt.Sprintf("%s/%s", path, bin)}...)
	if err := cmd.Start(); err != nil {
		return err
	}
	return nil
}

func restartIsulad() error {
54
	restart := selinuxCommand{"systemctl", []string{"restart", "isulad"}}
O
overweight 已提交
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
	cmd := exec.Command(restart.cmd, restart.argv...)
	logrus.Infof("%s %v", restart.cmd, restart.argv)
	if err := cmd.Run(); err != nil {
		logrus.Errorf("%s %v: %v", restart.cmd, restart.argv, err)
		return err
	}
	return nil
}

func relabel(path string) error {
	var seType string
	var attr string
	var err error

	if seType, err = utils.SeconfigGet(Seconfig, "SELINUXTYPE"); err != nil {
		return err
	}
	utils.SeconfigSet(Seconfig, "SELINUX", "permissive")
	fileContexts := fmt.Sprintf("/etc/selinux/%s/contexts/files/file_contexts.local", seType)
	if !utils.IsExist(fileContexts) {
		if err = ioutil.WriteFile(fileContexts, []byte(""), 0600); err != nil {
			logrus.Errorf("ioutil.WriteFile err: %s", err)
		}
	}

	preStarts := []selinuxCommand{
		{"setenforce", []string{"0"}},
	}

	for _, prestart := range preStarts {
		cmd := exec.Command(prestart.cmd, prestart.argv...)
		logrus.Infof("%s %v", prestart.cmd, prestart.argv)
		if err := cmd.Run(); err != nil {
			logrus.Errorf("%s %v: %v", prestart.cmd, prestart.argv, err)
			return err
		}
	}

	if attr, err = selinux.Getfilecon(hostSystemd); err != nil {
		return nil
	}
	modifyContexts := []selinuxContext{
97
		{"init_exec_t", path + "/isulad"},
O
overweight 已提交
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
	}
	con := utils.NewContext(attr)
	for _, context := range modifyContexts {
		con.SetType(context.conType)
		logrus.Infof("%s [%s]", context.path, con.Get())
		selinux.Setfilecon(context.path, con.Get())
	}

	return restartIsulad()
}

func setUpSelinuxLabel(path, rootfs string) error {
	if err := relabel(path); err != nil {
		return err
	}
	return nil
}

var relabelCommand = cli.Command{
	Name:        "relabel",
	Usage:       "relabel rootfs for running SELinux in system container",
	ArgsUsage:   `[--isulad-path path] [--rootfs rootfs]`,
	Description: `relabel rootfs for running SELinux in system container(a systemd based os is required)`,
	Flags: []cli.Flag{
		cli.StringFlag{
			Name:  "isulad-path",
			Value: "/usr/bin",
			Usage: "isulad's install path",
		},
		cli.StringFlag{
			Name:  "rootfs",
			Usage: "the absolute path for isulad's rootfs",
		},
	},
	Action: func(context *cli.Context) {
		if err := setUpSelinuxLabel(context.String("isulad-path"), context.String("rootfs")); err != nil {
			fatal(err)
		}
		// print result to stdout
		fmt.Printf("SELinux relabel success\n")
		logrus.Infof("SELinux relabel successfully")
	},
}