Skip to content

Commit ddc0cbe

Browse files
authored
Don't apply noexec mount flag during customization. (#263)
If the user applies the `noexec` to a partition's mount options, then add `noexec` to the `/etc/fstab` file but don't apply `noexec` during customization. This allows scripts like `grub2-mkconfig` to run during customization.
1 parent f808f27 commit ddc0cbe

4 files changed

Lines changed: 104 additions & 7 deletions

File tree

toolkit/tools/pkg/imagecustomizerlib/customizepartitions_test.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -401,16 +401,24 @@ func testCustomizeImageNewUUIDsHelper(t *testing.T, testName string, imageType b
401401
imageVersion)
402402
}
403403

404-
func verifyFstabEntries(t *testing.T, imageConnection *ImageConnection, mountPoints []mountPoint,
405-
partitions map[int]diskutils.PartitionInfo,
406-
) {
404+
func getFilteredFstabEntries(t *testing.T, imageConnection *ImageConnection) []diskutils.FstabEntry {
407405
fstabPath := filepath.Join(imageConnection.Chroot().RootDir(), "/etc/fstab")
408406
fstabEntries, err := diskutils.ReadFstabFile(fstabPath)
409407
if !assert.NoError(t, err, "read /etc/fstab") {
410-
return
408+
return nil
411409
}
412410

413411
filteredFstabEntries := filterOutSpecialPartitions(fstabEntries)
412+
return filteredFstabEntries
413+
}
414+
415+
func verifyFstabEntries(t *testing.T, imageConnection *ImageConnection, mountPoints []mountPoint,
416+
partitions map[int]diskutils.PartitionInfo,
417+
) {
418+
filteredFstabEntries := getFilteredFstabEntries(t, imageConnection)
419+
if filteredFstabEntries == nil {
420+
return
421+
}
414422

415423
if !assert.Equalf(t, len(mountPoints), len(filteredFstabEntries), "/etc/fstab entries count: %v", filteredFstabEntries) {
416424
return

toolkit/tools/pkg/imagecustomizerlib/customizeuki_test.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"path/filepath"
88
"testing"
99

10+
"github.com/microsoft/azurelinux/toolkit/tools/imagegen/diskutils"
1011
"github.com/microsoft/azurelinux/toolkit/tools/internal/file"
1112
"github.com/stretchr/testify/assert"
1213
"golang.org/x/sys/unix"
@@ -58,6 +59,11 @@ func TestCustomizeImageVerityUsrUki(t *testing.T) {
5859
FileSystemType: "ext4",
5960
Flags: unix.MS_RDONLY,
6061
},
62+
{
63+
PartitionNum: 6,
64+
Path: "/var",
65+
FileSystemType: "ext4",
66+
},
6167
}
6268

6369
imageConnection, err := connectToImage(buildDir, outImageFilePath, false /*includeDefaultMounts*/, mountPoints)
@@ -75,4 +81,59 @@ func TestCustomizeImageVerityUsrUki(t *testing.T) {
7581
usrHashDevice := partitionDevPath(imageConnection, 4)
7682
verifyVerityUki(t, espPath, usrDevice, usrHashDevice, "PARTUUID="+partitions[3].PartUuid,
7783
"PARTUUID="+partitions[4].PartUuid, "usr", buildDir, "rd.info", "panic-on-corruption")
84+
85+
expectedFstabEntries := []diskutils.FstabEntry{
86+
{
87+
Source: "PARTUUID=" + partitions[5].PartUuid,
88+
Target: "/",
89+
FsType: "ext4",
90+
Options: "noexec",
91+
VfsOptions: 0x8,
92+
FsOptions: "",
93+
Freq: 0,
94+
PassNo: 1,
95+
},
96+
{
97+
Source: "PARTUUID=" + partitions[2].PartUuid,
98+
Target: "/boot",
99+
FsType: "ext4",
100+
Options: "defaults",
101+
VfsOptions: 0x0,
102+
FsOptions: "",
103+
Freq: 0,
104+
PassNo: 2,
105+
},
106+
{
107+
Source: "PARTUUID=" + partitions[1].PartUuid,
108+
Target: "/boot/efi",
109+
FsType: "vfat",
110+
Options: "umask=0077",
111+
VfsOptions: 0x0,
112+
FsOptions: "umask=0077",
113+
Freq: 0,
114+
PassNo: 2,
115+
},
116+
{
117+
Source: "/dev/mapper/usr",
118+
Target: "/usr",
119+
FsType: "ext4",
120+
Options: "ro",
121+
VfsOptions: 0x1,
122+
FsOptions: "",
123+
Freq: 0,
124+
PassNo: 2,
125+
},
126+
{
127+
Source: "PARTUUID=" + partitions[6].PartUuid,
128+
Target: "/var",
129+
FsType: "ext4",
130+
Options: "defaults",
131+
VfsOptions: 0x0,
132+
FsOptions: "",
133+
Freq: 0,
134+
PassNo: 2,
135+
},
136+
}
137+
filteredFstabEntries := getFilteredFstabEntries(t, imageConnection)
138+
assert.Equal(t, expectedFstabEntries, filteredFstabEntries)
78139
}

toolkit/tools/pkg/imagecustomizerlib/partitionutils.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,9 @@ func fstabEntriesToMountPoints(fstabEntries []diskutils.FstabEntry, diskPartitio
215215
}
216216

217217
// Unset read-only flag so that read-only partitions can be customized.
218-
vfsOptions := fstabEntry.VfsOptions & ^diskutils.MountFlags(unix.MS_RDONLY)
218+
// Unset noexec flag so that if rootfs is set as noexec, image can still be customized. For example, allowing
219+
// grub2-mkconfig to be called.
220+
vfsOptions := fstabEntry.VfsOptions & ^diskutils.MountFlags(unix.MS_RDONLY|unix.MS_NOEXEC)
219221

220222
var mountPoint *safechroot.MountPoint
221223
if fstabEntry.Target == "/" {

toolkit/tools/pkg/imagecustomizerlib/testdata/verity-usr-uki.yaml

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ storage:
2020
size: 100M
2121

2222
- id: root
23-
size: 2G
23+
size: 500M
24+
25+
- id: var
26+
size: 1G
2427

2528
verity:
2629
- id: verityusr
@@ -42,14 +45,20 @@ storage:
4245

4346
- deviceId: root
4447
type: ext4
45-
mountPoint: /
48+
mountPoint:
49+
path: /
50+
options: noexec
4651

4752
- deviceId: verityusr
4853
type: ext4
4954
mountPoint:
5055
path: /usr
5156
options: ro
5257

58+
- deviceId: var
59+
type: ext4
60+
mountPoint: /var
61+
5362
os:
5463
bootloader:
5564
resetType: hard-reset
@@ -66,6 +75,23 @@ os:
6675
- grub2-efi-binary
6776

6877
install:
78+
- openssh-server
6979
- veritysetup
7080
- systemd-boot
7181
- device-mapper
82+
83+
users:
84+
- name: root
85+
password:
86+
type: plain-text
87+
value: hello
88+
89+
scripts:
90+
postCustomization:
91+
- content: |
92+
set -eux
93+
94+
# Move iptables script off of the / partition, so that the iptables service can run even though / has the noexec
95+
# mount flag.
96+
mv /etc/systemd/scripts /usr/bin/
97+
ln -sr /usr/bin/scripts /etc/systemd/scripts

0 commit comments

Comments
 (0)