Skip to content

Commit 9afddb4

Browse files
Merge branch 'master' of https://github.com/subgraph/paxrat (includes
@DerLat's PR)
2 parents 8d68b4f + bf68b28 commit 9afddb4

File tree

4 files changed

+67
-82
lines changed

4 files changed

+67
-82
lines changed

README.md

+2-4
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,10 @@ The following is an example configuration:
3636
```json
3737
{
3838
"/usr/lib/iceweasel/iceweasel": {
39-
"flags": "pm",
40-
"nonroot": false
39+
"flags": "pm"
4140
},
4241
"/usr/lib/iceweasel/plugin-container": {
43-
"flags": "m",
44-
"nonroot": false
42+
"flags": "m"
4543
},
4644
"/home/user/.local/share/torbrowser/tbb/x86_64/tor-browser_en-US/Browser/firefox": {
4745
"flags": "pm",

paxrat.conf

+24-48
Original file line numberDiff line numberDiff line change
@@ -1,97 +1,73 @@
11
{ "/usr/bin/gnome-shell": {
2-
"flags": "mr",
3-
"nonroot": false
2+
"flags": "mr"
43
},
54
"/usr/lib/gnome-session/gnome-session-check-accelerated": {
6-
"flags": "m",
7-
"nonroot": false
5+
"flags": "m"
86
},
97
"/usr/lib/gnome-session/gnome-session-check-accelerated-helper": {
10-
"flags": "m",
11-
"nonroot": false
8+
"flags": "m"
129
},
1310
"/usr/lib/gnome-session/gnome-session-failed": {
14-
"flags": "m",
15-
"nonroot": false
11+
"flags": "m"
1612
},
1713
"/usr/bin/seahorse": {
18-
"flags": "m",
19-
"nonroot": false
14+
"flags": "m"
2015
},
2116
"/usr/bin/grub-bios-setup": {
22-
"flags": "E",
23-
"nonroot": false
17+
"flags": "E"
2418
},
2519
"/usr/sbin/grub-mkdevicemap": {
26-
"flags": "E",
27-
"nonroot": false
20+
"flags": "E"
2821
},
2922
"/usr/sbin/grub-probe": {
30-
"flags": "E",
31-
"nonroot": false
23+
"flags": "E"
3224
},
3325
"/usr/bin/grub-script-check": {
34-
"flags": "E",
35-
"nonroot": false
26+
"flags": "E"
3627
},
3728
"/usr/bin/grub-mount": {
38-
"flags": "m",
39-
"nonroot": false
29+
"flags": "m"
4030
},
4131
"/usr/sbin/grub-probe": {
42-
"flags": "m",
43-
"nonroot": false
32+
"flags": "m"
4433
},
4534
"/usr/bin/mplayer": {
46-
"flags": "m",
47-
"nonroot": false
35+
"flags": "m"
4836
},
4937
"/usr/bin/python2": {
50-
"flags": "m",
51-
"nonroot": false
38+
"flags": "m"
5239
},
5340
"/usr/bin/python3": {
54-
"flags": "m",
55-
"nonroot": false
41+
"flags": "m"
5642
},
5743
"/usr/bin/vlc": {
58-
"flags": "m",
59-
"nonroot": false
44+
"flags": "m"
6045
},
6146
"/usr/lib/libreoffice/program/soffice.bin": {
62-
"flags": "m",
63-
"nonroot": false
47+
"flags": "m"
6448
},
6549
"/usr/lib/openoffice/program/unopkg.bin": {
66-
"flags": "m",
67-
"nonroot": false
50+
"flags": "m"
6851
},
6952
"/usr/lib/policy-kit-1/polkitd": {
70-
"flags": "mr",
71-
"nonroot": false
53+
"flags": "mr"
7254
},
7355
"/usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java": {
74-
"flags": "mr",
75-
"nonroot": false
56+
"flags": "mr"
7657
},
7758
"/usr/lib/jvm/java-7-openjdk-amd64/bin/javac": {
78-
"flags": "mr",
79-
"nonroot": false
59+
"flags": "mr"
8060
},
8161
"/usr/lib/jvm/java-7-openjdk-amd64/bin/jar": {
82-
"flags": "m",
83-
"nonroot": false
62+
"flags": "m"
8463
},
8564
"/usr/bin/gdbus": {
86-
"flags": "mr",
87-
"nonroot": false
65+
"flags": "mr"
8866
},
8967
"/usr/lib/iceweasel/iceweasel": {
90-
"flags": "pm",
91-
"nonroot": false
68+
"flags": "pm"
9269
},
9370
"/usr/lib/iceweasel/plugin-container": {
94-
"flags": "m",
95-
"nonroot": false
71+
"flags": "m"
9672
}
9773
}

paxrat.go

+29-17
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,46 @@
11
package main
22

33
import (
4-
"flag"
5-
"io/ioutil"
4+
"bufio"
65
"encoding/json"
7-
"regexp"
6+
"flag"
87
"fmt"
98
"log"
109
"log/syslog"
11-
"syscall"
1210
"os"
1311
"os/exec"
1412
"os/user"
1513
"path/filepath"
14+
"regexp"
1615
"strings"
16+
"syscall"
1717

1818
"golang.org/x/exp/inotify"
1919
)
2020

21-
2221
var configvar string
2322
var testvar bool
2423
var watchvar bool
2524
var flagsvar string
2625
var binaryvar string
2726
var nonrootvar bool
27+
2828
type Setting struct {
29-
Flags string `json:"flags"`
30-
Nonroot bool `json:"nonroot"`
29+
Flags string `json:"flags"`
30+
Nonroot bool `json:"nonroot,omitempty"`
3131
}
3232
type Config struct {
3333
Settings map[string]Setting
3434
}
35+
3536
var InotifyFlags uint32
3637
var InotifyDirFlags uint32
3738
var Conf *Config
3839
var LogWriter *syslog.Writer
3940
var SyslogError error
4041

42+
var commentRegexp = regexp.MustCompile("^[ \t]*#")
43+
4144
func init() {
4245
LogWriter, SyslogError = syslog.New(syslog.LOG_INFO, "paxrat")
4346
if SyslogError != nil {
@@ -57,7 +60,7 @@ func init() {
5760
"Test the config file and then exit")
5861
flag.BoolVar(&watchvar, "w", false,
5962
"Run paxrat in watch mode")
60-
flag.StringVar(&flagsvar, "s", "",
63+
flag.StringVar(&flagsvar, "s", "",
6164
"Set PaX flags for a single binary (must also specify binary)")
6265
flag.BoolVar(&nonrootvar, "n", false,
6366
"Set nonroot variable for a single binary (needed to set flags on a non-root owned binary")
@@ -66,12 +69,21 @@ func init() {
6669
}
6770

6871
func (conf *Config) readConfig(path string) (err error) {
69-
file, err := ioutil.ReadFile(path)
72+
file, err := os.Open(path)
7073
if err != nil {
7174
log.Fatal(err)
7275
}
76+
scanner := bufio.NewScanner(file)
77+
out := ""
78+
for scanner.Scan() {
79+
line := scanner.Text()
80+
if !commentRegexp.MatchString(line) {
81+
out += line + "\n"
82+
}
83+
84+
}
7385
var data = &conf.Settings
74-
err = json.Unmarshal(file, data)
86+
err = json.Unmarshal([]byte(out), data)
7587
if err != nil {
7688
log.Fatal(err)
7789
}
@@ -87,8 +99,8 @@ func pathExists(path string) (result bool) {
8799

88100
func validateFlags(flags string) (err error) {
89101
match, _ := regexp.MatchString("(?i)[^pemrxs]", flags)
90-
if match {
91-
err = fmt.Errorf("Bad characters found in PaX flags: %s",
102+
if match {
103+
err = fmt.Errorf("Bad characters found in PaX flags: %s",
92104
flags)
93105
}
94106
return
@@ -141,10 +153,10 @@ func setFlags(path string, flags string, nonroot bool) (err error) {
141153
}
142154
linkUid := fiPath.Sys().(*syscall.Stat_t).Uid
143155
// Throw error if nonroot option is not set but the file is owned by a user other than root
144-
if (!nonroot && linkUid > 0) {
156+
if !nonroot && linkUid > 0 {
145157
err = fmt.Errorf(
146158
"Cannot set PaX flags on %s. Owner of symlink did not match owner of symlink target\n",
147-
path)
159+
path)
148160
return
149161
}
150162
// Resolve the symlink target
@@ -161,7 +173,7 @@ func setFlags(path string, flags string, nonroot bool) (err error) {
161173
}
162174
targetUid := fiRPath.Sys().(*syscall.Stat_t).Uid
163175
// If nonroot is set then throw an error if the owner of the file is different than the owner of the symlink target
164-
if (nonroot && targetUid != linkUid) {
176+
if nonroot && targetUid != linkUid {
165177
err = fmt.Errorf(
166178
"Cannot set PaX flags on %s. Owner of symlink did not match owner of symlink target\n",
167179
path)
@@ -190,7 +202,7 @@ func setFlagsWatchMode(watcher *inotify.Watcher, path string, flags string, nonr
190202
watcher.RemoveWatch(path)
191203
setFlags(path, flags, nonroot)
192204
if err != nil {
193-
return(err)
205+
return (err)
194206
}
195207
addWatchToClosestPath(watcher, path)
196208
return
@@ -305,7 +317,7 @@ func runWatcher(watcher *inotify.Watcher) {
305317
msg := fmt.Sprintf("File created: %s\n", ev.Name)
306318
LogWriter.Info(msg)
307319
}
308-
// Catch directory creation events for non-existent directories in executable path
320+
// Catch directory creation events for non-existent directories in executable path
309321
} else if ev.Mask == (inotify.IN_CREATE | inotify.IN_ISDIR) {
310322
for path, _ := range (*Conf).Settings {
311323
if strings.HasPrefix(path, ev.Name) {

paxrat_test.go

+12-13
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package main
22

33
import (
4+
"fmt"
45
"io/ioutil"
56
"os"
67
"testing"
7-
"fmt"
88
"time"
99
)
1010

@@ -30,12 +30,12 @@ func TestRunWatcher1(t *testing.T) {
3030
}
3131
}
3232
testJson := fmt.Sprintf(
33-
"{\"%s/test1\": {" +
34-
"\"flags\": \"mr\"," +
35-
"\"nonroot\": false}," +
36-
"\"%s/test2\": {" +
37-
"\"flags\": \"E\"," +
38-
"\"nonroot\": false}}", dir, dir)
33+
`{"%s/test1": {`+
34+
`"flags": "mr",`+
35+
`"nonroot": false},`+
36+
`"%s/test2": {`+
37+
`"flags": "E",`+
38+
`"nonroot": false}}`, dir, dir)
3939
configPath := dir + "paxrat_conf.json"
4040
Conf = new(Config)
4141
err = createTestConfig(configPath, testJson)
@@ -58,7 +58,7 @@ func TestRunWatcher1(t *testing.T) {
5858
if err != nil {
5959
t.Fatalf("Could not remove testFile1: %s", err)
6060
}
61-
err = os.Rename(files[1], dir + "moved")
61+
err = os.Rename(files[1], dir+"moved")
6262
if err != nil {
6363
t.Fatalf("Could not move/rename TestFile2: %s", err)
6464
}
@@ -76,9 +76,9 @@ func TestRunWatcher2(t *testing.T) {
7676
}
7777
defer os.RemoveAll(dir)
7878
testJson := fmt.Sprintf(
79-
"{\"%s/1/2/3/4/5/6/7/8/9/10/test1\": {" +
80-
"\"flags\": \"mr\"," +
81-
"\"nonroot\": false}}", dir)
79+
`{"%s/1/2/3/4/5/6/7/8/9/10/test1": {`+
80+
`"flags": "mr",`+
81+
`"nonroot": false}}`, dir)
8282
configPath := dir + "paxrat_conf.json"
8383
Conf = new(Config)
8484
err = createTestConfig(configPath, testJson)
@@ -99,7 +99,7 @@ func TestRunWatcher2(t *testing.T) {
9999
runWatcher(watcher)
100100
}(done)
101101
time.Sleep(1 * time.Second)
102-
os.MkdirAll(dir + "/1/2/3/4/5/6/7/8/9/10", 0600 )
102+
os.MkdirAll(dir+"/1/2/3/4/5/6/7/8/9/10", 0600)
103103
time.Sleep(1 * time.Second)
104104
file := dir + "/1/2/3/4/5/6/7/8/9/10/test1"
105105
fmt.Printf("Creating test file: %s", file)
@@ -108,4 +108,3 @@ func TestRunWatcher2(t *testing.T) {
108108
t.Fatalf("creating test file: %s", err)
109109
}
110110
}
111-

0 commit comments

Comments
 (0)