Skip to content

Commit b06c7dd

Browse files
committed
Checksum robustness.
1 parent 5e1909a commit b06c7dd

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

vfs/cksm.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,12 @@ type cksmFlags struct {
4747

4848
func (c cksmFile) ReadAt(p []byte, off int64) (n int, err error) {
4949
n, err = c.File.ReadAt(p, off)
50+
p = p[:n]
5051

5152
// SQLite is reading the header of a database file.
5253
if c.isDB && off == 0 && len(p) >= 100 &&
5354
bytes.HasPrefix(p, []byte("SQLite format 3\000")) {
54-
c.init(p)
55+
c.init((*[100]byte)(p))
5556
}
5657

5758
// Verify checksums.
@@ -69,7 +70,7 @@ func (c cksmFile) WriteAt(p []byte, off int64) (n int, err error) {
6970
// SQLite is writing the first page of a database file.
7071
if c.isDB && off == 0 && len(p) >= 100 &&
7172
bytes.HasPrefix(p, []byte("SQLite format 3\000")) {
72-
c.init(p)
73+
c.init((*[100]byte)(p))
7374
}
7475

7576
// Compute checksums.
@@ -122,12 +123,16 @@ func (c cksmFile) fileControl(ctx context.Context, mod api.Module, op _FcntlOpco
122123
return vfsFileControlImpl(ctx, mod, c.File, op, pArg)
123124
}
124125

125-
func (f *cksmFlags) init(header []byte) {
126+
func (f *cksmFlags) init(header *[100]byte) {
126127
f.pageSize = 256 * int(binary.LittleEndian.Uint16(header[16:18]))
127128
if r := header[20] == 8; r != f.computeCksm {
128129
f.computeCksm = r
129130
f.verifyCksm = r
130131
}
132+
if !sql3util.ValidPageSize(f.pageSize) {
133+
f.computeCksm = false
134+
f.verifyCksm = false
135+
}
131136
}
132137

133138
func cksmCompute(a []byte) (cksm [8]byte) {

0 commit comments

Comments
 (0)