/
acls.go
571 lines (464 loc) · 16.8 KB
/
acls.go
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
54
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
97
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
package iptablesctrl
import (
"errors"
"fmt"
"strconv"
"strings"
"text/template"
"go.aporeto.io/trireme-lib/common"
"go.aporeto.io/trireme-lib/controller/constants"
"go.aporeto.io/trireme-lib/policy"
"go.uber.org/zap"
)
const (
tcpProto = "tcp"
udpProto = "udp"
numPackets = "100"
initialCount = "99"
)
type rulesInfo struct {
RejectObserveApply [][]string
RejectNotObserved [][]string
RejectObserveContinue [][]string
AcceptObserveApply [][]string
AcceptNotObserved [][]string
AcceptObserveContinue [][]string
ReverseRules [][]string
}
// cgroupChainRules provides the rules for redirecting to a processing unit
// specific chain based for Linux processed and based on the cgroups and net_cls
// configuration.
func (i *Instance) cgroupChainRules(cfg *ACLInfo) [][]string {
// Rules for older distros (eg RH 6.9/Ubuntu 14.04), due to absence of
// cgroup match modules, source ports are used to trap outgoing traffic.
if i.isLegacyKernel && (cfg.PUType == common.HostNetworkPU || cfg.PUType == common.HostPU) {
return i.legacyPuChainRules(
cfg.ContextID,
cfg.AppChain,
cfg.NetChain,
cfg.CgroupMark,
cfg.TCPPorts,
cfg.UDPPorts,
cfg.ProxyPort,
cfg.ProxySetName,
cfg.AppSection,
cfg.NetSection,
cfg.PUType,
)
}
tmpl := template.Must(template.New(cgroupCaptureTemplate).Funcs(template.FuncMap{
"isUDPPorts": func() bool {
return cfg.UDPPorts != "0"
},
"isTCPPorts": func() bool {
return cfg.TCPPorts != "0"
},
"isHostPU": func() bool {
return cfg.AppSection == HostModeOutput && cfg.NetSection == HostModeInput
},
}).Parse(cgroupCaptureTemplate))
rules, err := extractRulesFromTemplate(tmpl, cfg)
if err != nil {
zap.L().Warn("unable to extract rules", zap.Error(err))
}
return append(rules, i.proxyRules(cfg)...)
}
func (i *Instance) uidChainRules(cfg *ACLInfo) [][]string {
tmpl := template.Must(template.New(uidChainTemplate).Parse(uidChainTemplate))
rules, err := extractRulesFromTemplate(tmpl, cfg)
if err != nil {
zap.L().Warn("unable to extract rules", zap.Error(err))
}
if i.isLegacyKernel {
return append(rules, i.legacyProxyRules(cfg.TCPPorts, cfg.ProxyPort, cfg.ProxySetName, cfg.CgroupMark)...)
}
return append(rules, i.proxyRules(cfg)...)
}
// containerChainRules provides the list of rules that are used to send traffic to
// a particular chain
func (i *Instance) containerChainRules(cfg *ACLInfo) [][]string {
tmpl := template.Must(template.New(containerChainTemplate).Parse(containerChainTemplate))
rules, err := extractRulesFromTemplate(tmpl, cfg)
if err != nil {
zap.L().Warn("unable to extract rules", zap.Error(err))
}
return append(rules, i.proxyRules(cfg)...)
}
// proxyRules creates the rules that allow traffic to go through if it is handled
// by the services.
func (i *Instance) proxyRules(cfg *ACLInfo) [][]string {
tmpl := template.Must(template.New(proxyChainTemplate).Funcs(template.FuncMap{
"isCgroupSet": func() bool {
return cfg.CgroupMark != ""
},
}).Parse(proxyChainTemplate))
rules, err := extractRulesFromTemplate(tmpl, cfg)
if err != nil {
zap.L().Warn("unable to extract rules", zap.Error(err))
}
return rules
}
// trapRules provides the packet capture rules that are defined for each processing unit.
func (i *Instance) trapRules(cfg *ACLInfo, isHostPU bool) [][]string {
tmpl := template.Must(template.New(packetCaptureTemplate).Funcs(template.FuncMap{
"needDnsRules": func() bool {
return i.mode == constants.Sidecar || isHostPU || i.isLegacyKernel
},
"isUIDProcess": func() bool {
return cfg.UID != ""
},
}).Parse(packetCaptureTemplate))
rules, err := extractRulesFromTemplate(tmpl, cfg)
if err != nil {
zap.L().Warn("unable to extract rules", zap.Error(err))
}
return rules
}
// addContainerChain adds a chain for the specific container and redirects traffic there
// This simplifies significantly the management and makes the iptable rules more readable
// All rules related to a container are contained within the dedicated chain
func (i *Instance) addContainerChain(appChain string, netChain string) error {
if err := i.ipt.NewChain(i.appPacketIPTableContext, appChain); err != nil {
return fmt.Errorf("unable to add chain %s of context %s: %s", appChain, i.appPacketIPTableContext, err)
}
if err := i.ipt.NewChain(i.netPacketIPTableContext, netChain); err != nil {
return fmt.Errorf("unable to add netchain %s of context %s: %s", netChain, i.netPacketIPTableContext, err)
}
return nil
}
// processRulesFromList is a generic helper that parses a set of rules and sends the corresponding
// ACL commands.
func (i *Instance) processRulesFromList(rulelist [][]string, methodType string) error {
var err error
for _, cr := range rulelist {
// HACK: Adding a retry loop to avoid iptables error of "invalid argument"
// Once in a while iptables
L:
for retry := 0; retry < 3; retry++ {
switch methodType {
case "Append":
if err = i.ipt.Append(cr[0], cr[1], cr[2:]...); err == nil {
break L
}
case "Insert":
order, err := strconv.Atoi(cr[2])
if err != nil {
zap.L().Error("Incorrect format for iptables insert")
return errors.New("invalid format")
}
if err = i.ipt.Insert(cr[0], cr[1], order, cr[3:]...); err == nil {
break L
}
case "Delete":
if err = i.ipt.Delete(cr[0], cr[1], cr[2:]...); err == nil {
break L
}
default:
return errors.New("invalid method type")
}
}
if err != nil && methodType != "Delete" {
return fmt.Errorf("unable to %s rule for table %s and chain %s with error %s", methodType, cr[0], cr[1], err)
}
}
return nil
}
// addChainrules implements all the iptable rules that redirect traffic to a chain
func (i *Instance) addChainRules(cfg *ACLInfo) error {
if i.mode != constants.LocalServer {
return i.processRulesFromList(i.containerChainRules(cfg), "Append")
}
if cfg.UID != "" {
return i.processRulesFromList(i.uidChainRules(cfg), "Append")
}
return i.processRulesFromList(i.cgroupChainRules(cfg), "Append")
}
// addPacketTrap adds the necessary iptables rules to capture control packets to user space
func (i *Instance) addPacketTrap(cfg *ACLInfo, isHostPU bool) error {
return i.processRulesFromList(i.trapRules(cfg, isHostPU), "Append")
}
func (i *Instance) generateACLRules(contextID string, rule *aclIPset, chain string, reverseChain string, nfLogGroup, proto, ipMatchDirection string, reverseDirection string) ([][]string, [][]string) {
iptRules := [][]string{}
reverseRules := [][]string{}
observeContinue := rule.policy.ObserveAction.ObserveContinue()
baseRule := func(proto string) []string {
iptRule := []string{
i.appPacketIPTableContext,
chain,
"-p", proto,
"-m", "set", "--match-set", rule.ipset, ipMatchDirection,
}
if proto == constants.TCPProtoNum || proto == constants.TCPProtoString {
stateMatch := []string{"-m", "state", "--state", "NEW"}
iptRule = append(iptRule, stateMatch...)
}
// only tcp uses target networks
if proto == constants.TCPProtoNum || proto == constants.TCPProtoString {
targetNet := []string{"-m", "set", "!", "--match-set", targetTCPNetworkSet, ipMatchDirection}
iptRule = append(iptRule, targetNet...)
}
// port match is required only for tcp and udp protocols
if proto == constants.TCPProtoNum || proto == constants.UDPProtoNum || proto == constants.TCPProtoString || proto == constants.UDPProtoString {
portMatchSet := []string{"--match", "multiport", "--dports", strings.Join(rule.ports, ",")}
iptRule = append(iptRule, portMatchSet...)
}
return iptRule
}
if rule.policy.Action&policy.Log > 0 || observeContinue {
nflog := []string{"-m", "state", "--state", "NEW",
"-j", "NFLOG", "--nflog-group", nfLogGroup, "--nflog-prefix", rule.policy.LogPrefix(contextID)}
nfLogRule := append(baseRule(proto), nflog...)
iptRules = append(iptRules, nfLogRule)
}
if !observeContinue {
if (rule.policy.Action & policy.Accept) != 0 {
accept := []string{"-j", "ACCEPT"}
acceptRule := append(baseRule(proto), accept...)
iptRules = append(iptRules, acceptRule)
}
if rule.policy.Action&policy.Reject != 0 {
reject := []string{"-j", "DROP"}
rejectRule := append(baseRule(proto), reject...)
iptRules = append(iptRules, rejectRule)
}
if rule.policy.Action&policy.Accept != 0 && (proto == constants.UDPProtoNum || proto == constants.UDPProtoString) {
reverseRules = append(reverseRules, []string{
i.appPacketIPTableContext,
reverseChain,
"-p", proto,
"-m", "set", "--match-set", rule.ipset, reverseDirection,
"-m", "state", "--state", "ESTABLISHED",
"-j", "ACCEPT",
})
}
}
return iptRules, reverseRules
}
// sortACLsInBuckets will process all the rules and add them in a list of buckets
// based on their priority. We need an explicit order of these buckets
// in order to support observation only of ACL actions. The parameters
// must provide the chain and whether it is App or Net ACLs so that the rules
// can be created accordingly.
func (i *Instance) sortACLsInBuckets(contextID, chain string, reverseChain string, rules []aclIPset, isAppACLs bool) *rulesInfo {
rulesBucket := &rulesInfo{
RejectObserveApply: [][]string{},
RejectNotObserved: [][]string{},
RejectObserveContinue: [][]string{},
AcceptObserveApply: [][]string{},
AcceptNotObserved: [][]string{},
AcceptObserveContinue: [][]string{},
ReverseRules: [][]string{},
}
direction := "src"
reverse := "dst"
nflogGroup := "11"
if isAppACLs {
direction = "dst"
reverse = "src"
nflogGroup = "10"
}
for _, rule := range rules {
for _, proto := range rule.protocols {
acls, r := i.generateACLRules(contextID, &rule, chain, reverseChain, nflogGroup, proto, direction, reverse)
rulesBucket.ReverseRules = append(rulesBucket.ReverseRules, r...)
if testReject(rule.policy) && testObserveApply(rule.policy) {
rulesBucket.RejectObserveApply = append(rulesBucket.RejectObserveApply, acls...)
}
if testReject(rule.policy) && testNotObserved(rule.policy) {
rulesBucket.RejectNotObserved = append(rulesBucket.RejectNotObserved, acls...)
}
if testReject(rule.policy) && testObserveContinue(rule.policy) {
rulesBucket.RejectObserveContinue = append(rulesBucket.RejectObserveContinue, acls...)
}
if testAccept(rule.policy) && testObserveContinue(rule.policy) {
rulesBucket.AcceptObserveContinue = append(rulesBucket.AcceptObserveContinue, acls...)
}
if testAccept(rule.policy) && testNotObserved(rule.policy) {
rulesBucket.AcceptNotObserved = append(rulesBucket.AcceptNotObserved, acls...)
}
if testAccept(rule.policy) && testObserveApply(rule.policy) {
rulesBucket.AcceptObserveApply = append(rulesBucket.AcceptObserveApply, acls...)
}
}
}
return rulesBucket
}
// addExternalACLs adds a set of rules to the external services that are initiated
// by an application. The allow rules are inserted with highest priority.
func (i *Instance) addExternalACLs(contextID string, chain string, reverseChain string, rules []aclIPset, isAppAcls bool) error {
rulesBucket := i.sortACLsInBuckets(contextID, chain, reverseChain, rules, isAppAcls)
tmpl := template.Must(template.New(acls).Funcs(template.FuncMap{
"joinRule": func(rule []string) string {
return strings.Join(rule, " ")
},
}).Parse(acls))
aclRules, err := extractRulesFromTemplate(tmpl, *rulesBucket)
if err != nil {
return fmt.Errorf("unable to extract rules from template: %s", err)
}
if err := i.processRulesFromList(aclRules, "Append"); err != nil {
return fmt.Errorf("unable to install rules - mode :%s %v", err, isAppAcls)
}
return nil
}
// deleteChainRules deletes the rules that send traffic to our chain
func (i *Instance) deleteChainRules(cfg *ACLInfo) error {
if i.mode != constants.LocalServer {
return i.processRulesFromList(i.containerChainRules(cfg), "Delete")
}
if cfg.UID != "" {
return i.processRulesFromList(i.uidChainRules(cfg), "Delete")
}
return i.processRulesFromList(i.cgroupChainRules(cfg), "Delete")
}
// deletePUChains removes all the container specific chains and basic rules
func (i *Instance) deletePUChains(appChain, netChain string) error {
if err := i.ipt.ClearChain(i.appPacketIPTableContext, appChain); err != nil {
zap.L().Warn("Failed to clear the container ack packets chain",
zap.String("appChain", appChain),
zap.String("context", i.appPacketIPTableContext),
zap.Error(err),
)
}
if err := i.ipt.DeleteChain(i.appPacketIPTableContext, appChain); err != nil {
zap.L().Warn("Failed to delete the container ack packets chain",
zap.String("appChain", appChain),
zap.String("context", i.appPacketIPTableContext),
zap.Error(err),
)
}
if err := i.ipt.ClearChain(i.netPacketIPTableContext, netChain); err != nil {
zap.L().Warn("Failed to clear the container net packets chain",
zap.String("netChain", netChain),
zap.String("context", i.netPacketIPTableContext),
zap.Error(err),
)
}
if err := i.ipt.DeleteChain(i.netPacketIPTableContext, netChain); err != nil {
zap.L().Warn("Failed to delete the container net packets chain",
zap.String("netChain", netChain),
zap.String("context", i.netPacketIPTableContext),
zap.Error(err),
)
}
return nil
}
// setGlobalRules installs the global rules
func (i *Instance) setGlobalRules() error {
cfg, err := i.newACLInfo(0, "", nil, 0)
if err != nil {
return err
}
tmpl := template.Must(template.New(globalRules).Funcs(template.FuncMap{
"isLocalServer": func() bool {
return i.mode == constants.LocalServer
},
}).Parse(globalRules))
rules, err := extractRulesFromTemplate(tmpl, cfg)
if err != nil {
zap.L().Warn("unable to extract rules", zap.Error(err))
}
if err := i.processRulesFromList(rules, "Append"); err != nil {
return fmt.Errorf("unable to install global rules:%s", err)
}
// nat rules cannot be templated, since they interfere with Docker.
err = i.ipt.Insert(i.appProxyIPTableContext,
ipTableSectionPreRouting, 1,
"-p", "tcp",
"-m", "addrtype", "--dst-type", "LOCAL",
"-m", "set", "!", "--match-set", excludedNetworkSet, "src",
"-j", natProxyInputChain)
if err != nil {
return fmt.Errorf("unable to add default allow for marked packets at net: %s", err)
}
err = i.ipt.Insert(i.appProxyIPTableContext,
ipTableSectionOutput, 1,
"-m", "set", "!", "--match-set", excludedNetworkSet, "dst",
"-j", natProxyOutputChain)
if err != nil {
return fmt.Errorf("unable to add default allow for marked packets at net: %s", err)
}
return nil
}
func (i *Instance) removeGlobalHooks(cfg *ACLInfo) error {
tmpl := template.Must(template.New(globalHooks).Funcs(template.FuncMap{
"isLocalServer": func() bool {
return i.mode == constants.LocalServer
},
}).Parse(globalHooks))
rules, err := extractRulesFromTemplate(tmpl, cfg)
if err != nil {
return fmt.Errorf("unable to create trireme chains:%s", err)
}
i.processRulesFromList(rules, "Delete") // nolint
return nil
}
func (i *Instance) cleanACLs() error { // nolint
cfg, err := i.newACLInfo(0, "", nil, 0)
if err != nil {
return err
}
// First clear the nat rules
if err := i.removeGlobalHooks(cfg); err != nil {
zap.L().Error("unable to remove nat proxy rules")
}
tmpl := template.Must(template.New(deleteChains).Funcs(template.FuncMap{
"isLocalServer": func() bool {
return i.mode == constants.LocalServer
},
}).Parse(deleteChains))
rules, err := extractRulesFromTemplate(tmpl, cfg)
if err != nil {
return fmt.Errorf("unable to create trireme chains:%s", err)
}
for _, rule := range rules {
if len(rule) != 4 {
continue
}
// Flush the chains
if rule[2] == "-F" {
if err := i.ipt.ClearChain(rule[1], rule[3]); err != nil {
zap.L().Error("unable to flush chain", zap.String("table", rule[1]), zap.String("chain", rule[3]), zap.Error(err))
}
}
// Delete the chains
if rule[2] == "-X" {
if err := i.ipt.DeleteChain(rule[1], rule[3]); err != nil {
zap.L().Error("unable to delete chain", zap.String("table", rule[1]), zap.String("chain", rule[3]), zap.Error(err))
}
}
}
// Clean Application Rules/Chains
i.cleanACLSection(i.appPacketIPTableContext, chainPrefix)
i.ipt.Commit() // nolint
// Always return nil here. No reason to block anything if cleans fail.
return nil
}
// cleanACLSection flushes and deletes all chains with Prefix - Trireme
func (i *Instance) cleanACLSection(context, chainPrefix string) {
rules, err := i.ipt.ListChains(context)
if err != nil {
zap.L().Warn("Failed to list chains",
zap.String("context", context),
zap.Error(err),
)
}
for _, rule := range rules {
if strings.Contains(rule, chainPrefix) {
if err := i.ipt.ClearChain(context, rule); err != nil {
zap.L().Warn("Can not clear the chain",
zap.String("context", context),
zap.String("section", rule),
zap.Error(err),
)
}
if err := i.ipt.DeleteChain(context, rule); err != nil {
zap.L().Warn("Can not delete the chain",
zap.String("context", context),
zap.String("section", rule),
zap.Error(err),
)
}
}
}
}