github.com/google/syzkaller@v0.0.0-20240517125934-c0f1611a36d6/pkg/subsystem/linux/names.go (about)

     1  // Copyright 2023 syzkaller project authors. All rights reserved.
     2  // Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
     3  
     4  package linux
     5  
     6  import (
     7  	"fmt"
     8  	"regexp"
     9  	"strings"
    10  
    11  	"github.com/google/syzkaller/pkg/subsystem"
    12  )
    13  
    14  // setSubsystemNames assigns unique names to the presented subsystems.
    15  // If it failed to assign a name to a subsystem, the Name field remains empty.
    16  func setSubsystemNames(list []*subsystem.Subsystem) error {
    17  	dupEmails := map[string]string{}
    18  	for _, item := range list {
    19  		if item.Name == "" {
    20  			continue
    21  		}
    22  		if _, ok := dupEmails[item.Name]; ok {
    23  			return fmt.Errorf("duplicate name: %q", item.Name)
    24  		}
    25  		// We do not know the email.
    26  		dupEmails[item.Name] = ""
    27  	}
    28  	for _, item := range list {
    29  		if item.Name != "" {
    30  			continue
    31  		}
    32  		// For now, we can only infer name from the list email.
    33  		if len(item.Lists) == 0 {
    34  			return fmt.Errorf("no lists for %#v", item)
    35  		}
    36  		email := item.Lists[0]
    37  		name := emailToName(email)
    38  		if !validateName(name) {
    39  			return fmt.Errorf("failed to extract a name from %s", email)
    40  		}
    41  		if other, ok := dupEmails[name]; ok {
    42  			return fmt.Errorf("duplicate subsystem name %v: emails %q and %q", name, other, email)
    43  		}
    44  		item.Name = name
    45  		dupEmails[name] = email
    46  	}
    47  	return nil
    48  }
    49  
    50  func validateName(name string) bool {
    51  	const (
    52  		minLen = 2
    53  		maxLen = 16 // otherwise the email subject can get too big
    54  	)
    55  	return len(name) >= minLen && len(name) <= maxLen
    56  }
    57  
    58  func emailToName(email string) string {
    59  	if name := emailExceptions[email]; name != "" {
    60  		return name
    61  	}
    62  	ret := emailStripRe.FindStringSubmatch(email)
    63  	if ret == nil {
    64  		return ""
    65  	}
    66  	return strings.ReplaceAll(ret[1], ".", "")
    67  }
    68  
    69  func buildEmailStripRe() *regexp.Regexp {
    70  	raw := `^(?:`
    71  	for i := 0; i < len(stripPrefixes); i++ {
    72  		if i > 0 {
    73  			raw += "|"
    74  		}
    75  		raw += regexp.QuoteMeta(stripPrefixes[i])
    76  	}
    77  	raw += ")*(.*?)(?:"
    78  	for i := 0; i < len(stripSuffixes); i++ {
    79  		if i > 0 {
    80  			raw += "|"
    81  		}
    82  		raw += regexp.QuoteMeta(stripSuffixes[i])
    83  	}
    84  	raw += ")*@.*$"
    85  	return regexp.MustCompile(raw)
    86  }
    87  
    88  var (
    89  	emailExceptions = map[string]string{
    90  		"patches@opensource.cirrus.com":             "cirrus",
    91  		"virtualization@lists.linux-foundation.org": "virt", // the name is too long
    92  		"virtualization@lists.linux.dev":            "virt",
    93  		"dev@openvswitch.org":                       "openvswitch",
    94  		"devel@acpica.org":                          "acpica",
    95  		"kernel@dh-electronics.com":                 "dh-electr",
    96  		"devel@lists.orangefs.org":                  "orangefs",
    97  		"linux-arm-kernel@axis.com":                 "axis",
    98  		"Dell.Client.Kernel@dell.com":               "dell",
    99  		"sound-open-firmware@alsa-project.org":      "sof",
   100  		"platform-driver-x86@vger.kernel.org":       "x86-drivers",
   101  		"linux-trace-devel@vger.kernel.org":         "rt-tools",
   102  		"aws-nitro-enclaves-devel@amazon.com":       "nitro",
   103  		"brcm80211-dev-list.pdl@broadcom.com":       "brcm80211",
   104  		"osmocom-net-gprs@lists.osmocom.org":        "osmocom",
   105  		"netdev@vger.kernel.org":                    "net",
   106  		"megaraidlinux.pdl@broadcom.com":            "megaraid",
   107  		"mpi3mr-linuxdrv.pdl@broadcom.com":          "mpi3",
   108  		"MPT-FusionLinux.pdl@broadcom.com":          "mpt-fusion",
   109  		"linux-security-module@vger.kernel.org":     "lsm",       // the original name is too long
   110  		"linux-unionfs@vger.kernel.org":             "overlayfs", // the name has changed
   111  		"rust-for-linux@vger.kernel.org":            "rust",
   112  		"industrypack-devel@lists.sourceforge.net":  "ipack",
   113  		"v9fs-developer@lists.sourceforge.net":      "9p",
   114  		"kernel-tls-handshake@lists.linux.dev":      "tls",
   115  		"bcm-kernel-feedback-list@broadcom.com":     "broadcom",
   116  		"linux@ew.tq-group.com":                     "tq-systems",
   117  		"linux-imx@nxp.com":                         "nxp",
   118  	}
   119  	stripPrefixes = []string{"linux-"}
   120  	stripSuffixes = []string{
   121  		"-devel", "-dev", "-devs", "-developer", "devel",
   122  		"-user", "-users",
   123  		"-discussion", "-discuss", "-list", "-en", "-bugreport", "list",
   124  		"-kernel", "-linux", "-general", "-platform",
   125  	}
   126  	emailStripRe = buildEmailStripRe()
   127  )