Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stricter Fat32 volume validation, fix Fat32 MkDir when volume name matches new directory name #160

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions filesystem/fat32/directory.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fat32

import (
"fmt"
"time"
)

Expand Down Expand Up @@ -72,6 +73,10 @@ func (d *Directory) createEntry(name string, cluster uint32, dir bool) (*directo

// createVolumeLabel create a volume label entry in the given directory, and return the handle to it
func (d *Directory) createVolumeLabel(name string) (*directoryEntry, error) {
name, err := validateAndFormatVolumeLabel(name)
if err != nil {
return nil, fmt.Errorf("Unable to create a volume label entry: %w", err)
}
// allocate a slot for the new filename in the existing directory
entry := directoryEntry{
filenameLong: "",
Expand Down
34 changes: 30 additions & 4 deletions filesystem/fat32/fat32.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fat32

import (
"bytes"
"errors"
"fmt"
"os"
Expand Down Expand Up @@ -626,14 +627,36 @@ func (fs *FileSystem) Label() string {
return labelEntry.filenameShort + labelEntry.fileExtension
}

// SetLabel changes the filesystem label
func (fs *FileSystem) SetLabel(volumeLabel string) error {
// https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file#naming-conventions
// https://www.cs.fsu.edu/~cop4610t/assignments/project3/spec/fatspec.pdf
func validateAndFormatVolumeLabel(volumeLabel string) (string, error) {
if volumeLabel == "" {
volumeLabel = "NO NAME"
}
if len(volumeLabel) > 11 {
return "", fmt.Errorf("Volume label |%s| must be less than 11 characters, got %d", volumeLabel, len(volumeLabel))
}

// ensure the volumeLabel is proper sized
volumeLabel = fmt.Sprintf("%-11.11s", volumeLabel)
// The following characters are not legal in any bytes of DIR_Name:
// • Values less than 0x20 except for the special case of 0x05 in DIR_Name[0] described above.
// TODO
// • 0x22, 0x2A, 0x2B, 0x2C, 0x2E, 0x2F, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F, 0x5B, 0x5C, 0x5D,
// and 0x7C.
invalidBytes := string([]byte{0x22, 0x2A, 0x2B, 0x2C, 0x2E, 0x2F, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F, 0x5B, 0x5C, 0x5D, 0x7C})
idx := bytes.IndexAny([]byte(volumeLabel), invalidBytes)
if idx != -1 {
return "", fmt.Errorf("Volume label cannot have invalid character %c", volumeLabel[idx])
}

return fmt.Sprintf("%-11.11s", volumeLabel), nil
}

// SetLabel changes the filesystem label
func (fs *FileSystem) SetLabel(volumeLabel string) error {
volumeLabel, err := validateAndFormatVolumeLabel(volumeLabel)
if err != nil {
return fmt.Errorf("Error validating volume label %w", err)
}

// set the label in the superblock
bpb := fs.bootSector.biosParameterBlock
Expand Down Expand Up @@ -827,6 +850,9 @@ func (fs *FileSystem) readDirWithMkdir(p string, doMake bool) (*Directory, []*di
// do we have an entry whose name is the same as this name?
found := false
for _, e := range entries {
if e.isVolumeLabel {
continue
}
if e.filenameLong != subp && e.filenameShort != subp && (!e.lowercaseShortname || (e.lowercaseShortname && !strings.EqualFold(e.filenameShort, subp))) {
continue
}
Expand Down
21 changes: 21 additions & 0 deletions filesystem/fat32/fat32_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,3 +348,24 @@ func TestFat32ReadDirWithMkdir(t *testing.T) {
}
}
}

func TestFat32ValidateAndFormatVolumeLabel(t *testing.T) {
label, err := validateAndFormatVolumeLabel("foo")
if err != nil {
t.Fatal(err)
}
if label != "foo " {
t.Fatal("invalid format")
}

label, err = validateAndFormatVolumeLabel("ahfkjsdhfkjshdkjfhkhsdf")
if err == nil {
t.Fatal("expected error validating input over accepted length")
}

label, err = validateAndFormatVolumeLabel("*")
if err == nil {
t.Fatal("expected error validating input with bad character")
}

}
14 changes: 14 additions & 0 deletions filesystem/fat32/fat32_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,20 @@ func TestFat32Type(t *testing.T) {
}

func TestFat32Mkdir(t *testing.T) {
t.Run("Creating directory at root with the same name as volume", func(t *testing.T) {
f, err := os.CreateTemp("", "*")
label := "boot"
// create an empty filesystem
fs, err := fat32.Create(f, 1000000, 0, 512, label)
if err != nil {
t.Fatalf("error creating fat32 filesystem: %v", err)
}
err = fs.Mkdir("/" + label)
if err != nil {
t.Fatalf("error creating directory with same name as label (%s): %v", label, err)
}
})

// only do this test if os.Getenv("TEST_IMAGE") contains a real image
if intImage == "" {
return
Expand Down