mirror of https://github.com/subgraph/fw-daemon
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
322 lines
6.1 KiB
322 lines
6.1 KiB
9 years ago
|
package main
|
||
|
|
||
|
import (
|
||
|
"encoding/binary"
|
||
|
"fmt"
|
||
9 years ago
|
"io/ioutil"
|
||
9 years ago
|
"net"
|
||
9 years ago
|
"os"
|
||
|
"path"
|
||
|
"strconv"
|
||
9 years ago
|
"strings"
|
||
|
"unicode"
|
||
|
|
||
|
"github.com/subgraph/fw-daemon/nfqueue"
|
||
9 years ago
|
"github.com/subgraph/go-procsnitch"
|
||
9 years ago
|
)
|
||
|
|
||
|
const (
|
||
|
RULE_DENY = iota
|
||
|
RULE_ALLOW
|
||
|
)
|
||
|
|
||
|
const matchAny = 0
|
||
|
const noAddress = uint32(0xffffffff)
|
||
|
|
||
8 years ago
|
type RuleMode uint16
|
||
|
|
||
|
const (
|
||
|
RULE_MODE_SESSION RuleMode = iota
|
||
|
RULE_MODE_PERMANENT
|
||
|
RULE_MODE_SYSTEM
|
||
|
)
|
||
|
|
||
9 years ago
|
type Rule struct {
|
||
8 years ago
|
id uint
|
||
|
policy *Policy
|
||
|
mode RuleMode
|
||
|
rtype int
|
||
|
hostname string
|
||
|
addr uint32
|
||
|
port uint16
|
||
9 years ago
|
}
|
||
|
|
||
|
func (r *Rule) String() string {
|
||
9 years ago
|
return r.getString(false)
|
||
|
}
|
||
|
|
||
|
func (r *Rule) getString(redact bool) string {
|
||
9 years ago
|
rtype := "DENY"
|
||
9 years ago
|
if r.rtype == RULE_ALLOW {
|
||
|
rtype = "ALLOW"
|
||
|
}
|
||
8 years ago
|
rmode := ""
|
||
|
if r.mode == RULE_MODE_SYSTEM {
|
||
|
rmode = "|SYSTEM"
|
||
|
}
|
||
9 years ago
|
|
||
8 years ago
|
return fmt.Sprintf("%s|%s%s", rtype, r.AddrString(redact), rmode)
|
||
9 years ago
|
}
|
||
|
|
||
9 years ago
|
func (r *Rule) AddrString(redact bool) string {
|
||
9 years ago
|
addr := "*"
|
||
|
port := "*"
|
||
9 years ago
|
if r.hostname != "" {
|
||
|
addr = r.hostname
|
||
|
} else if r.addr != matchAny && r.addr != noAddress {
|
||
|
bs := make([]byte, 4)
|
||
|
binary.BigEndian.PutUint32(bs, r.addr)
|
||
|
addr = fmt.Sprintf("%d.%d.%d.%d", bs[0], bs[1], bs[2], bs[3])
|
||
|
}
|
||
|
|
||
|
if r.port != matchAny {
|
||
|
port = fmt.Sprintf("%d", r.port)
|
||
|
}
|
||
|
|
||
9 years ago
|
if redact && addr != "*" {
|
||
|
addr = "[redacted]"
|
||
|
}
|
||
|
|
||
9 years ago
|
return fmt.Sprintf("%s:%s", addr, port)
|
||
9 years ago
|
}
|
||
|
|
||
|
type RuleList []*Rule
|
||
|
|
||
9 years ago
|
func (r *Rule) match(dst net.IP, dstPort uint16, hostname string) bool {
|
||
|
if r.port != matchAny && r.port != dstPort {
|
||
9 years ago
|
return false
|
||
|
}
|
||
|
if r.addr == matchAny {
|
||
|
return true
|
||
|
}
|
||
|
if r.hostname != "" {
|
||
9 years ago
|
return r.hostname == hostname
|
||
9 years ago
|
}
|
||
9 years ago
|
return r.addr == binary.BigEndian.Uint32(dst)
|
||
9 years ago
|
}
|
||
|
|
||
|
type FilterResult int
|
||
|
|
||
|
const (
|
||
|
FILTER_DENY FilterResult = iota
|
||
|
FILTER_ALLOW
|
||
|
FILTER_PROMPT
|
||
|
)
|
||
|
|
||
9 years ago
|
func (rl *RuleList) filterPacket(p *nfqueue.Packet, pinfo *procsnitch.Info, hostname string) FilterResult {
|
||
8 years ago
|
return rl.filter(p, p.Dst, p.DstPort, hostname, pinfo)
|
||
9 years ago
|
}
|
||
|
|
||
8 years ago
|
func (rl *RuleList) filter(pkt *nfqueue.Packet, dst net.IP, dstPort uint16, hostname string, pinfo *procsnitch.Info) FilterResult {
|
||
9 years ago
|
if rl == nil {
|
||
|
return FILTER_PROMPT
|
||
|
}
|
||
|
result := FILTER_PROMPT
|
||
|
for _, r := range *rl {
|
||
9 years ago
|
if r.match(dst, dstPort, hostname) {
|
||
|
dstStr := dst.String()
|
||
8 years ago
|
if FirewallConfig.LogRedact {
|
||
9 years ago
|
dstStr = "[redacted]"
|
||
9 years ago
|
}
|
||
8 years ago
|
srcStr := "[uknown]"
|
||
|
if pkt != nil {
|
||
|
srcStr = fmt.Sprintf("%s:%d", pkt.Src, pkt.SrcPort)
|
||
|
}
|
||
|
log.Noticef("%s > %s %s %s -> %s:%d",
|
||
|
r.getString(FirewallConfig.LogRedact),
|
||
|
pinfo.ExePath, "TCP",
|
||
|
srcStr,
|
||
|
dstStr, dstPort)
|
||
9 years ago
|
if r.rtype == RULE_DENY {
|
||
|
return FILTER_DENY
|
||
|
} else if r.rtype == RULE_ALLOW {
|
||
|
result = FILTER_ALLOW
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return result
|
||
|
}
|
||
|
|
||
|
func parseError(s string) error {
|
||
|
return fmt.Errorf("unable to parse rule string: %s", s)
|
||
|
}
|
||
|
|
||
|
func (r *Rule) parse(s string) bool {
|
||
|
r.addr = noAddress
|
||
|
parts := strings.Split(s, "|")
|
||
8 years ago
|
if len(parts) < 2 {
|
||
9 years ago
|
return false
|
||
|
}
|
||
8 years ago
|
if len(parts) >= 3 && parts[2] == "SYSTEM" {
|
||
|
r.mode = RULE_MODE_SYSTEM
|
||
|
}
|
||
9 years ago
|
return r.parseVerb(parts[0]) && r.parseTarget(parts[1])
|
||
|
}
|
||
|
|
||
|
func (r *Rule) parseVerb(v string) bool {
|
||
|
switch v {
|
||
|
case "ALLOW":
|
||
|
r.rtype = RULE_ALLOW
|
||
|
return true
|
||
|
case "DENY":
|
||
|
r.rtype = RULE_DENY
|
||
|
return true
|
||
|
}
|
||
|
return false
|
||
|
}
|
||
|
|
||
|
func (r *Rule) parseTarget(t string) bool {
|
||
|
addrPort := strings.Split(t, ":")
|
||
|
if len(addrPort) != 2 {
|
||
|
return false
|
||
|
}
|
||
|
return r.parseAddr(addrPort[0]) && r.parsePort(addrPort[1])
|
||
|
}
|
||
|
|
||
|
func (r *Rule) parseAddr(a string) bool {
|
||
|
if a == "*" {
|
||
|
r.hostname = ""
|
||
|
r.addr = matchAny
|
||
|
return true
|
||
|
}
|
||
|
if strings.IndexFunc(a, unicode.IsLetter) != -1 {
|
||
|
r.hostname = a
|
||
|
return true
|
||
|
}
|
||
|
ip := net.ParseIP(a)
|
||
9 years ago
|
if ip == nil {
|
||
9 years ago
|
return false
|
||
|
}
|
||
9 years ago
|
r.addr = binary.BigEndian.Uint32(ip.To4())
|
||
9 years ago
|
return true
|
||
|
}
|
||
|
|
||
|
func (r *Rule) parsePort(p string) bool {
|
||
|
if p == "*" {
|
||
|
r.port = matchAny
|
||
|
return true
|
||
|
}
|
||
|
var err error
|
||
|
port, err := strconv.ParseUint(p, 10, 16)
|
||
9 years ago
|
if err != nil || port == 0 || port > 0xFFFF {
|
||
9 years ago
|
return false
|
||
|
}
|
||
|
r.port = uint16(port)
|
||
|
return true
|
||
|
}
|
||
|
|
||
9 years ago
|
const ruleFile = "/var/lib/sgfw/sgfw_rules"
|
||
9 years ago
|
|
||
9 years ago
|
func maybeCreateDir(dir string) error {
|
||
9 years ago
|
_, err := os.Stat(dir)
|
||
9 years ago
|
if os.IsNotExist(err) {
|
||
|
return os.MkdirAll(dir, 0755)
|
||
9 years ago
|
}
|
||
9 years ago
|
return err
|
||
|
}
|
||
|
|
||
|
func rulesPath() (string, error) {
|
||
|
if err := maybeCreateDir(path.Dir(ruleFile)); err != nil {
|
||
|
return ruleFile, err
|
||
|
}
|
||
|
return ruleFile, nil
|
||
9 years ago
|
}
|
||
|
|
||
|
func (fw *Firewall) saveRules() {
|
||
|
fw.lock.Lock()
|
||
|
defer fw.lock.Unlock()
|
||
|
|
||
9 years ago
|
p, err := rulesPath()
|
||
9 years ago
|
if err != nil {
|
||
8 years ago
|
log.Warningf("Failed to open %s for writing: %v", p, err)
|
||
9 years ago
|
return
|
||
|
}
|
||
|
f, err := os.Create(p)
|
||
|
if err != nil {
|
||
8 years ago
|
log.Warningf("Failed to open %s for writing: %v", p, err)
|
||
9 years ago
|
return
|
||
|
}
|
||
|
defer f.Close()
|
||
|
|
||
|
for _, p := range fw.policies {
|
||
|
savePolicy(f, p)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func savePolicy(f *os.File, p *Policy) {
|
||
|
p.lock.Lock()
|
||
|
defer p.lock.Unlock()
|
||
|
if !p.hasPersistentRules() {
|
||
|
return
|
||
|
}
|
||
|
|
||
|
if !writeLine(f, "["+p.path+"]") {
|
||
|
return
|
||
|
}
|
||
|
for _, r := range p.rules {
|
||
8 years ago
|
if r.mode != RULE_MODE_SESSION {
|
||
9 years ago
|
if !writeLine(f, r.String()) {
|
||
|
return
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func writeLine(f *os.File, line string) bool {
|
||
|
_, err := f.WriteString(line + "\n")
|
||
|
if err != nil {
|
||
8 years ago
|
log.Warningf("Error writing to rule file: %v", err)
|
||
9 years ago
|
return false
|
||
|
}
|
||
|
return true
|
||
|
}
|
||
|
|
||
|
func (fw *Firewall) loadRules() {
|
||
|
fw.lock.Lock()
|
||
|
defer fw.lock.Unlock()
|
||
|
|
||
9 years ago
|
fw.clearRules()
|
||
|
|
||
|
p, err := rulesPath()
|
||
9 years ago
|
if err != nil {
|
||
8 years ago
|
log.Warningf("Failed to open %s for reading: %v", p, err)
|
||
9 years ago
|
return
|
||
|
}
|
||
|
bs, err := ioutil.ReadFile(p)
|
||
9 years ago
|
if err != nil {
|
||
|
if !os.IsNotExist(err) {
|
||
8 years ago
|
log.Warningf("Failed to open %s for reading: %v", p, err)
|
||
9 years ago
|
}
|
||
|
return
|
||
|
}
|
||
|
var policy *Policy
|
||
|
for _, line := range strings.Split(string(bs), "\n") {
|
||
|
if strings.HasPrefix(line, "[") && strings.HasSuffix(line, "]") {
|
||
|
policy = fw.processPathLine(line)
|
||
9 years ago
|
} else if len(strings.TrimSpace(line)) > 0 {
|
||
9 years ago
|
processRuleLine(policy, line)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (fw *Firewall) processPathLine(line string) *Policy {
|
||
|
path := line[1 : len(line)-1]
|
||
|
policy := fw.policyForPath(path)
|
||
|
policy.lock.Lock()
|
||
|
defer policy.lock.Unlock()
|
||
|
policy.rules = nil
|
||
|
return policy
|
||
|
}
|
||
|
|
||
|
func processRuleLine(policy *Policy, line string) {
|
||
|
if policy == nil {
|
||
8 years ago
|
log.Warningf("Cannot process rule line without first seeing path line: %s", line)
|
||
9 years ago
|
return
|
||
|
}
|
||
9 years ago
|
_, err := policy.parseRule(line, true)
|
||
9 years ago
|
if err != nil {
|
||
8 years ago
|
log.Warningf("Error parsing rule (%s): %v", line, err)
|
||
9 years ago
|
return
|
||
|
}
|
||
|
}
|