Most efficent way to check for world writable files

Hello. I’m a Golang noobie and this is one of my first apps.

The goal is to walk through an entire file system and find all the files that are world-writable. The script should take less than 5 minutes. This code works but we have some really large filesystems which can take up to 10 minutes to walk.

Is there a better way to check for the file mode and is there a most efficient method of doing the checks? Would there be any benefit to using concurrency?

// FilePathWalkDir accepts a root dir and then returns a
// string array of files that are world writable by everyone.
func FilePathWalkDir() ([]string, error) {
	var files []string

	err := filepath.WalkDir("/", func(path string, d os.DirEntry, err error) error {
		// If the path is a directory and one of the skip dirs then skip it.
		if d.IsDir() {
			switch skipPath := path; skipPath {
			case "/proc":
				return filepath.SkipDir
			case "/run":
				return filepath.SkipDir
			case "/sys/fs":
				return filepath.SkipDir
			case "/selinux":
				return filepath.SkipDir
			}
		}

		// If the type of entry is a regular file then get the permissions and if it's world writable
		// add it to the files array.
		if d.Type().IsRegular() {
			info, _ := d.Info()
			s := string(info.Mode().String()[8])
			matched, _ := regexp.MatchString(`w`, s)

			if matched {
				files = append(files, path)
			}
		}
		return nil
	})
	// return the files array
	return files, err
}

Why aren’t you checking using bit operations?

I’m not 100% what you mean. I only seem to be able to get -rw-r–r--. Is there an easier way to rather get the octal or binary values or do I need to do the conversion myself?

info.Mode()s last 9 bit represent the permissions which is documented here: https://pkg.go.dev/io/fs#FileMode

So info.Mode() && o777 will give you the perms only, info.Mode() && o100 != 0 will be true for any owner executable file, info.Mode() && o004 != 0 for all world writeable files.

Call * FilePathWalkDir* concurrently could help.