Skip to content

Commit f8e19b8

Browse files
authored
Merge pull request #17 from jean-edouard/ptrace
xattr: add cap_sys_ptrace to supported capabilities
2 parents a597c85 + 139e436 commit f8e19b8

File tree

6 files changed

+65
-4
lines changed

6 files changed

+65
-4
lines changed

.bazelversion

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3.1.0
1+
4.2.1

cmd/xattr.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func NewXATTRCmd() *cobra.Command {
5555
}
5656
tarWriter := tar.NewWriter(streamOutput)
5757
defer tarWriter.Close()
58-
return xattr.Apply(tar.NewReader(streamInput), tarWriter , capabilityMap, labelMap)
58+
return xattr.Apply(tar.NewReader(streamInput), tarWriter, capabilityMap, labelMap)
5959
},
6060
}
6161

pkg/xattr/testdata/regenerate.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/usr/bin/env bash
2+
3+
TMPDIR=$(mktemp -d)
4+
trap 'rm -rf "${TMPDIR}"' EXIT
5+
6+
touch ${TMPDIR}/selinux
7+
touch ${TMPDIR}/cap_net_bind_service
8+
touch ${TMPDIR}/cap_chown
9+
touch ${TMPDIR}/cap_sys_ptrace
10+
touch ${TMPDIR}/cap_all
11+
sudo chcon -t user_home_t ${TMPDIR}/selinux
12+
13+
sudo setcap 'cap_net_bind_service=+ep' ${TMPDIR}/cap_net_bind_service
14+
sudo setcap 'cap_chown=+ep' ${TMPDIR}/cap_chown
15+
sudo setcap 'cap_sys_ptrace=+ep' ${TMPDIR}/cap_sys_ptrace
16+
sudo setcap 'cap_net_bind_service,cap_chown,cap_sys_ptrace=+ep' ${TMPDIR}/cap_all
17+
tar -C ${TMPDIR} --xattrs -cvf xattr.tar .

pkg/xattr/testdata/xattr.tar

0 Bytes
Binary file not shown.

pkg/xattr/xattr.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ const (
1313

1414
var cap_empty_bitmask = []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
1515
var supported_capabilities = map[string][]byte{
16+
"cap_chown": {1, 0, 0, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
1617
"cap_net_bind_service": {1, 0, 0, 2, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
18+
"cap_sys_ptrace": {1, 0, 0, 2, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
1719
}
1820

1921
func AddCapabilities(pax map[string]string, capabilities []string) error {
@@ -37,7 +39,7 @@ func SetSELinuxLabel(pax map[string]string, label string) error {
3739
if label == "" {
3840
return fmt.Errorf("label must not be empty, but got '%s'", label)
3941
}
40-
pax[selinux_header] = fmt.Sprintf("%s\x00",label)
42+
pax[selinux_header] = fmt.Sprintf("%s\x00", label)
4143
return nil
4244
}
4345

pkg/xattr/xattr_test.go

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ var g *GomegaWithT
1414

1515
func TestSettingSELinuxLabel(t *testing.T) {
1616
g = NewGomegaWithT(t)
17-
referenceEntry, err := getHeader("blub")
17+
referenceEntry, err := getHeader("./selinux")
1818
g.Expect(err).ToNot(HaveOccurred())
1919

2020
generatedEntry := &tar.Header{Name: "blub"}
@@ -43,3 +43,45 @@ func getHeader(name string) (*tar.Header, error) {
4343
}
4444
return nil, fmt.Errorf("entry %v does not exist", name)
4545
}
46+
47+
func Test_Capabilities(t *testing.T) {
48+
tests := []struct {
49+
name string
50+
entry string
51+
capabilities []string
52+
}{
53+
{
54+
name: "should set cap_chown",
55+
entry: "./cap_chown",
56+
capabilities: []string{"cap_chown"},
57+
},
58+
{
59+
name: "should set cap_net_bind_service",
60+
entry: "./cap_net_bind_service",
61+
capabilities: []string{"cap_net_bind_service"},
62+
},
63+
{
64+
name: "should set cap_sys_ptrace",
65+
entry: "./cap_sys_ptrace",
66+
capabilities: []string{"cap_sys_ptrace"},
67+
},
68+
{
69+
name: "should set all implemented capabilities",
70+
entry: "./cap_all",
71+
capabilities: []string{"cap_chown", "cap_net_bind_service", "cap_sys_ptrace"},
72+
},
73+
}
74+
for _, tt := range tests {
75+
t.Run(tt.name, func(t *testing.T) {
76+
g = NewGomegaWithT(t)
77+
referenceEntry, err := getHeader(tt.entry)
78+
g.Expect(err).ToNot(HaveOccurred())
79+
80+
generatedEntry := &tar.Header{Name: "blub"}
81+
82+
g.Expect(enrichEntry(generatedEntry, map[string][]string{"blub": tt.capabilities}, nil)).To(Succeed())
83+
84+
g.Expect(generatedEntry.PAXRecords[capabilities_header]).To(Equal(referenceEntry.PAXRecords[capabilities_header]))
85+
})
86+
}
87+
}

0 commit comments

Comments
 (0)