Debian Codegraph @

go/Makefile

raw
1 # Building the libcap/{cap.psx} Go packages, and examples.
2 #
3 # Note, we use symlinks to construct a go.mod build friendly tree. The
4 # packages themselves are intended to be (ultimately) found via proxy
5 # as "kernel.org/pub/linux/libs/security/libcap/cap" and
6 # "kernel.org/pub/linux/libs/security/libcap/psx". However, to
7 # validate their use on these paths, we fake such a structure in the
8 # build tree with symlinks and a vendor directory.
9
10 topdir=$(realpath ..)
11 include $(topdir)/Make.Rules
12
13 IMPORTDIR=kernel.org/pub/linux/libs/security/libcap
14 PKGDIR=pkg/$(GOOSARCH)/$(IMPORTDIR)
15
16 DEPS=../libcap/libcap.a ../libcap/libpsx.a
17 TESTS=compare-cap try-launching psx-signals mismatch
18
19 all: PSXGOPACKAGE CAPGOPACKAGE web setid gowns captree captrace
20
21 vet: PSXGOPACKAGE CAPGOPACKAGE
22 CC="$(CC)" CGO_ENABLED="$(CGO_REQUIRED)" $(GO) vet -mod=vendor kernel.org/pub/linux/libs/security/libcap/cap
23 CC="$(CC)" CGO_ENABLED="$(CGO_REQUIRED)" $(GO) vet -mod=vendor kernel.org/pub/linux/libs/security/libcap/psx
24
25 $(DEPS):
26 $(MAKE) -C ../libcap all
27
28 ../progs/tcapsh-static:
29 $(MAKE) -C ../progs tcapsh-static
30
31 vendor/$(IMPORTDIR):
32 mkdir -p "vendor/$(IMPORTDIR)"
33
34 vendor/modules.txt: vendor/$(IMPORTDIR)
35 echo "# $(IMPORTDIR)/psx v$(GOMAJOR).$(VERSION).$(MINOR)" > vendor/modules.txt
36 echo "$(IMPORTDIR)/psx" >> vendor/modules.txt
37 echo "# $(IMPORTDIR)/cap v$(GOMAJOR).$(VERSION).$(MINOR)" >> vendor/modules.txt
38 echo "$(IMPORTDIR)/cap" >> vendor/modules.txt
39
40 vendor/$(IMPORTDIR)/psx: vendor/modules.txt
41 ln -sf $(topdir)/psx vendor/$(IMPORTDIR)
42 touch ../psx
43
44 vendor/$(IMPORTDIR)/cap: vendor/modules.txt
45 ln -sf $(topdir)/cap vendor/$(IMPORTDIR)
46 touch ../cap
47
48 $(topdir)/libcap/cap_names.h:
49 $(MAKE) -C $(topdir)/libcap cap_names.h
50
51 good-names.go: $(topdir)/libcap/cap_names.h vendor/$(IMPORTDIR)/cap mknames.go
52 CC="$(BUILD_CC)" GOOS= GOARCH= $(GO) run -mod=vendor mknames.go --header=$< --textdir=$(topdir)/doc/values | gofmt > $@ || rm -f $@
53 diff -u ../cap/names.go $@
54
55 PSXGOPACKAGE: vendor/$(IMPORTDIR)/psx ../psx/*.go $(DEPS)
56 touch $@
57
58 CAPGOPACKAGE: vendor/$(IMPORTDIR)/cap ../cap/*.go good-names.go $(PSXGOPACKAGE)
59 touch $@
60
61 # Compiles something with this package to compare it to libcap. This
62 # tests more when run under sudotest (see ../progs/quicktest.sh for that).
63 compare-cap: compare-cap.go CAPGOPACKAGE
64 CC="$(CC)" CGO_ENABLED="1" CGO_CFLAGS="$(CGO_CFLAGS)" CGO_LDFLAGS="$(CGO_LDFLAGS)" $(GO) build $(GO_BUILD_FLAGS) -mod=vendor $<
65
66 web: ../goapps/web/web.go CAPGOPACKAGE
67 CC="$(CC)" CGO_ENABLED="$(CGO_REQUIRED)" $(GO) build $(GO_BUILD_FLAGS) -mod=vendor -o $@ $<
68 ifeq ($(RAISE_GO_FILECAP),yes)
69 $(MAKE) -C ../progs setcap
70 $(SUDO) ../progs/setcap cap_setpcap,cap_net_bind_service=p web
71 @echo "NOTE: RAISED cap_setpcap,cap_net_bind_service ON web binary"
72 endif
73
74 setid: ../goapps/setid/setid.go CAPGOPACKAGE PSXGOPACKAGE
75 CC="$(CC)" CGO_ENABLED="$(CGO_REQUIRED)" $(GO) build $(GO_BUILD_FLAGS) -mod=vendor -o $@ $<
76
77 gowns: ../goapps/gowns/gowns.go CAPGOPACKAGE
78 CC="$(CC)" CGO_ENABLED="$(CGO_REQUIRED)" $(GO) build $(GO_BUILD_FLAGS) -mod=vendor -o $@ $<
79
80 captree: ../goapps/captree/captree.go CAPGOPACKAGE
81 CC="$(CC)" CGO_ENABLED="$(CGO_REQUIRED)" $(GO) build $(GO_BUILD_FLAGS) -mod=vendor -o $@ $<
82
83 captrace: ../goapps/captrace/captrace.go CAPGOPACKAGE
84 CC="$(CC)" CGO_ENABLED="$(CGO_REQUIRED)" $(GO) build $(GO_BUILD_FLAGS) -mod=vendor -o $@ $<
85
86 ok: ok.go vendor/modules.txt
87 CC="$(CC)" CGO_ENABLED="0" $(GO) build $(GO_BUILD_FLAGS) -mod=vendor $<
88
89 try-launching: try-launching.go CAPGOPACKAGE ok
90 CC="$(CC)" CGO_ENABLED="$(CGO_REQUIRED)" $(GO) build $(GO_BUILD_FLAGS) -mod=vendor $<
91 ifeq ($(CGO_REQUIRED),0)
92 CC="$(CC)" CGO_ENABLED="1" $(GO) build $(GO_BUILD_FLAGS) -mod=vendor -o $@-cgo $<
93 endif
94
95 # This is a test case developed from the deadlock investigation,
96 # https://github.com/golang/go/issues/50113 . Note the psx-fd.go code
97 # works when compiled CGO_ENABLED=1, but deadlocks when compiled
98 # CGO_ENABLED=0. This is true for go1.16 and go1.17. The go1.18
99 # release fixed this by rewriting the AllThreadsSyscall support, but
100 # the large change was not backported. (See noted bug for a much
101 # smaller patch for this issue on those older releases.)
102 psx-fd: psx-fd.go PSXGOPACKAGE
103 CC="$(CC)" CGO_ENABLED="$(CGO_REQUIRED)" $(GO) build $(GO_BUILD_FLAGS) -mod=vendor -o $@ $<
104
105 ifeq ($(CGO_REQUIRED),0)
106 psx-fd-cgo: psx-fd.go PSXGOPACKAGE
107 CC="$(CC)" CGO_ENABLED="1" $(GO) build $(GO_BUILD_FLAGS) -mod=vendor -o $@ $<
108 endif
109
110 psx-signals: psx-signals.go PSXGOPACKAGE
111 CC="$(CC)" CGO_ENABLED="$(CGO_REQUIRED)" CGO_CFLAGS="$(CGO_CFLAGS)" CGO_LDFLAGS="$(CGO_LDFLAGS)" $(GO) build $(GO_BUILD_FLAGS) -mod=vendor $<
112
113 ifeq ($(CGO_REQUIRED),0)
114 psx-signals-cgo: psx-signals.go PSXGOPACKAGE
115 CC="$(CC)" CGO_ENABLED="1" CGO_CFLAGS="$(CGO_CFLAGS)" CGO_LDFLAGS="$(CGO_LDFLAGS)" $(GO) build $(GO_BUILD_FLAGS) -mod=vendor -o $@ $<
116 endif
117
118 b210613: b210613.go CAPGOPACKAGE
119 CC="$(CC)" CGO_ENABLED="$(CGO_REQUIRED)" CGO_CFLAGS="$(CGO_CFLAGS)" CGO_LDFLAGS="$(CGO_LDFLAGS)" $(GO) build $(GO_BUILD_FLAGS) -mod=vendor $<
120
121 b215283: b215283.go CAPGOPACKAGE
122 CC="$(CC)" CGO_ENABLED="$(CGO_REQUIRED)" CGO_CFLAGS="$(CGO_CFLAGS)" CGO_LDFLAGS="$(CGO_LDFLAGS)" $(GO) build $(GO_BUILD_FLAGS) -mod=vendor $<
123
124 ifeq ($(CGO_REQUIRED),0)
125 b215283-cgo: b215283.go CAPGOPACKAGE
126 CC="$(CC)" CGO_ENABLED="1" CGO_CFLAGS="$(CGO_CFLAGS)" CGO_LDFLAGS="$(CGO_LDFLAGS)" $(GO) build $(GO_BUILD_FLAGS) -mod=vendor -o $@ $<
127 endif
128
129 mismatch: mismatch.go PSXGOPACKAGE
130 CC="$(CC)" CGO_ENABLED="$(CGO_REQUIRED)" CGO_CFLAGS="$(CGO_CFLAGS)" CGO_LDFLAGS="$(CGO_LDFLAGS)" $(GO) build $(GO_BUILD_FLAGS) -mod=vendor $<
131
132 iaber: iaber.go CAPGOPACKAGE PSXGOPACKAGE
133 CC="$(CC)" CGO_ENABLED="$(CGO_REQUIRED)" CGO_CFLAGS="$(CGO_CFLAGS)" CGO_LDFLAGS="$(CGO_LDFLAGS)" $(GO) build $(GO_BUILD_FLAGS) -mod=vendor $<
134
135 ifeq ($(CGO_REQUIRED),0)
136 mismatch-cgo: mismatch.go CAPGOPACKAGE
137 CC="$(CC)" CGO_ENABLED="1" CGO_CFLAGS="$(CGO_CFLAGS)" CGO_LDFLAGS="$(CGO_LDFLAGS)" $(GO) build $(GO_BUILD_FLAGS) -mod=vendor -o $@ $<
138 endif
139
140 test: vet setid gowns captree psx-fd $(TESTS)
141 CC="$(CC)" CGO_ENABLED="$(CGO_REQUIRED)" $(GO) test -v -mod=vendor $(IMPORTDIR)/psx
142 ifeq ($(CGO_REQUIRED),0)
143 CC="$(CC)" CGO_ENABLED="1" $(GO) test -v -mod=vendor $(IMPORTDIR)/psx
144 CC="$(CC)" CGO_ENABLED="1" $(GO) test -v -mod=vendor $(IMPORTDIR)/cap
145 endif
146 CC="$(CC)" CGO_ENABLED="$(CGO_REQUIRED)" $(GO) test -v -mod=vendor $(IMPORTDIR)/cap
147 LD_LIBRARY_PATH=../libcap ./compare-cap
148 ./psx-signals
149 ./mismatch || exit 0 ; exit 1
150 timeout 5 ./psx-fd || echo "this is a known Go bug"
151 ifeq ($(CGO_REQUIRED),0)
152 $(MAKE) psx-signals-cgo mismatch-cgo psx-fd-cgo
153 ./psx-signals-cgo
154 ./mismatch-cgo || exit 0 ; exit 1
155 ./psx-fd-cgo
156 endif
157 ./setid --caps=false
158 ./gowns -- -c "echo gowns runs"
159 ./captree 0
160
161 # Note, the user namespace doesn't require sudo, but I wanted to avoid
162 # requiring that the hosting kernel supports user namespaces for the
163 # regular test case.
164 sudotest: test ../progs/tcapsh-static b210613 b215283 iaber
165 ../progs/tcapsh-static --has-b=cap_sys_admin || exit 0 && ./gowns --ns -- -c "echo gowns runs with user namespace"
166 ./try-launching
167 ifeq ($(CGO_REQUIRED),0)
168 ./try-launching-cgo
169 endif
170 ifeq ($(CGO_REQUIRED),0)
171 CC="$(CC)" CGO_ENABLED="1" $(SUDO) $(GO) test -v -mod=vendor $(IMPORTDIR)/cap
172 endif
173 CC="$(CC)" CGO_ENABLED="$(CGO_REQUIRED)" $(SUDO) $(GO) test -v -mod=vendor $(IMPORTDIR)/cap
174 $(SUDO) ./try-launching
175 ifeq ($(CGO_REQUIRED),0)
176 $(SUDO) ./try-launching-cgo
177 endif
178 $(SUDO) ../progs/tcapsh-static --cap-uid=$$(id -u) --caps="cap_setpcap=ep" --iab="^cap_setpcap" -- -c ./b210613
179 $(SUDO) ./b215283
180 ifeq ($(CGO_REQUIRED),0)
181 $(MAKE) b215283-cgo
182 $(SUDO) ./b215283-cgo
183 endif
184 @echo should pass:
185 $(SUDO) ./iaber '!^cap_setgid,^cap_setuid,^cap_setpcap' '!^cap_setuid,^cap_setgid'
186 @echo should fail:
187 $(SUDO) ./iaber '!^cap_setgid,^cap_setuid' '!^cap_setuid,^cap_setgid' || exit 0 ; exit 1
188
189 # As of libcap-2.55 We stopped installing the cap and psx packages as
190 # part of the install. Most distribution's packagers skip the Go
191 # builds, so it was not well used any way. The new hotness is to just
192 # use Go modules and download the packages from a tagged release in
193 # the git repository. For an example of how to do this from scratch:
194 #
195 # https://sites.google.com/site/fullycapable/getting-started-with-go/building-go-programs-that-manipulate-capabilities
196 #
197 # For those brave souls that do include the Go build (testing) as part
198 # of their packaging, we reward them with a copy of the captree
199 # utility!
200 install: all
201 mkdir -p -m 0755 $(FAKEROOT)$(SBINDIR)
202 install -m 0755 captree $(FAKEROOT)$(SBINDIR)
203
204 clean:
205 rm -f *.o *.so *~ mknames ok good-names.go
206 rm -f web setid gowns captree captrace iaber
207 rm -f compare-cap try-launching try-launching-cgo
208 rm -f $(topdir)/cap/*~ $(topdir)/psx/*~
209 rm -f b210613 b215283 b215283-cgo psx-signals psx-signals-cgo
210 rm -f mismatch mismatch-cgo psx-fd psx-fd-cgo
211 rm -fr vendor CAPGOPACKAGE PSXGOPACKAGE go.sum