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

✨ New Probe: Memory safety #4499

Merged
merged 55 commits into from
Feb 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
a875096
draft code
balteravishay Dec 27, 2024
821e8d8
Fix sorting contributors before comparing in test (#4455)
spencerschrock Dec 12, 2024
8047295
:seedling: Bump actions/setup-go from 5.1.0 to 5.2.0 (#4445)
dependabot[bot] Dec 16, 2024
8a2004f
:seedling: Bump golang.org/x/crypto from 0.29.0 to 0.31.0 (#4460)
dependabot[bot] Dec 16, 2024
a9c3737
Downgrade osv-scanner to v1.9.0 (#4461)
spencerschrock Dec 17, 2024
06d78cb
:seedling: Bump the github-actions group across 1 directory with 3 up…
dependabot[bot] Dec 23, 2024
688500f
:seedling: Bump the gomod group across 2 directories with 4 updates (…
dependabot[bot] Dec 23, 2024
5dc1514
update definition.yaml
balteravishay Jan 9, 2025
4b578fa
remove prominent languages code
balteravishay Jan 21, 2025
8977acc
tests
balteravishay Jan 21, 2025
c1419a8
more implementation details
balteravishay Jan 22, 2025
5522abc
:seedling: Bump the gomod group across 2 directories with 3 updates (…
dependabot[bot] Dec 31, 2024
95407bc
:sparkles: implement more of the Azure DevOps client (#4456)
JamieMagee Dec 31, 2024
e379792
:seedling: Bump github.com/google/osv-scanner from 1.9.0 to 1.9.2 (#4…
dependabot[bot] Jan 2, 2025
f8f4e2c
:seedling: Bump github.com/rhysd/actionlint from 1.7.4 to 1.7.6 (#4478)
dependabot[bot] Jan 6, 2025
0c096bf
:seedling: Bump github.com/golangci/golangci-lint from 1.62.2 to 1.63…
dependabot[bot] Jan 6, 2025
bab3e61
:seedling: Bump the gomod group across 2 directories with 6 updates (…
dependabot[bot] Jan 6, 2025
52564c0
📖 governance: Add meeting note archives from 2021 through 2024 (#4482)
justaugustus Jan 6, 2025
7926e28
Enables scanning of open source dependencies used by Chromium. (#4476)
renewitt Jan 7, 2025
3dc104c
Update Metal3 repos in projects.csv (#4442)
tuminoid Jan 7, 2025
6ff8bc3
:seedling: Logging a warning if readGitHubTokens finds several values…
aunovis-heidrich Jan 9, 2025
73a5cd2
:seedling: Bump the gomod group across 2 directories with 3 updates (…
dependabot[bot] Jan 17, 2025
dcdaf56
:seedling: Bump the github-actions group across 1 directory with 5 up…
dependabot[bot] Jan 17, 2025
3c6c20b
:seedling: Bump the distroless group across 6 directories with 1 upda…
dependabot[bot] Jan 17, 2025
3470be3
:seedling: Bump the golang group across 8 directories with 1 update (…
dependabot[bot] Jan 21, 2025
b0b0ecf
:seedling: Bump github.com/rhysd/actionlint from 1.7.6 to 1.7.7 (#4495)
dependabot[bot] Jan 21, 2025
5e00565
:seedling: Bump the github-actions group across 1 directory with 2 up…
dependabot[bot] Jan 21, 2025
e4080e6
:seedling: Bump the gomod group across 2 directories with 4 updates (…
dependabot[bot] Jan 21, 2025
2812e97
more coverage
balteravishay Jan 22, 2025
18856a7
more tests
balteravishay Jan 23, 2025
6421907
fix list
balteravishay Jan 23, 2025
62023c7
fix search in map
balteravishay Jan 24, 2025
dbbe114
:seedling: Bump the github-actions group with 2 updates (#4503)
dependabot[bot] Jan 28, 2025
0416bfe
:seedling: Bump actions/setup-go from 5.2.0 to 5.3.0 (#4504)
dependabot[bot] Jan 28, 2025
8ba9a03
:seedling: Bump github.com/golang/glog from 1.2.2 to 1.2.4 (#4507)
dependabot[bot] Jan 28, 2025
06fa25a
:seedling: Bump github/codeql-action in the github-actions group (#4509)
dependabot[bot] Feb 3, 2025
d732856
pr comments
balteravishay Feb 5, 2025
f4b1863
:seedling: Bump the github-actions group with 4 updates (#4513)
dependabot[bot] Feb 10, 2025
bb1ef7f
:seedling: Bump the golang group across 8 directories with 1 update (…
dependabot[bot] Feb 10, 2025
4ea69a4
:seedling: Bump the gomod group across 2 directories with 8 updates (…
dependabot[bot] Feb 10, 2025
00f5845
:seedling: remove OS and Arch info from scorecard release binary name…
timothysparg Feb 12, 2025
2480be0
:sparkles: Add GitHub git compatibility mode (#4474)
spencerschrock Feb 12, 2025
c3f340c
Cache files during gitfile handler setup (#4522)
spencerschrock Feb 16, 2025
7fe8b2a
change to be specific for unsafe blocks
balteravishay Feb 17, 2025
7458007
:seedling: Bump the gomod group across 2 directories with 3 updates (…
dependabot[bot] Dec 31, 2024
217bcc2
:seedling: Bump github.com/golangci/golangci-lint from 1.62.2 to 1.63…
dependabot[bot] Jan 6, 2025
a4f2ca8
:seedling: Bump the gomod group across 2 directories with 4 updates (…
dependabot[bot] Feb 18, 2025
7f3e731
:seedling: Bump goreleaser/goreleaser-action from 6.1.0 to 6.2.1 (#4524)
dependabot[bot] Feb 18, 2025
ff9b74d
:seedling: Bump the golang group across 8 directories with 1 update (…
dependabot[bot] Feb 18, 2025
1448536
:seedling: Bump github.com/golangci/golangci-lint from 1.63.4 to 1.64…
dependabot[bot] Feb 18, 2025
f0e54b1
:seedling: Bump the github-actions group across 1 directory with 3 up…
dependabot[bot] Feb 18, 2025
d537a44
pr fixes
balteravishay Feb 26, 2025
136c3f3
Merge branch 'main' into memory-safety-probe
balteravishay Feb 26, 2025
4bae2ed
probes.md
balteravishay Feb 26, 2025
0aa68f4
return finding.OutcomeError when encountering parse errors
spencerschrock Feb 27, 2025
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
14 changes: 14 additions & 0 deletions docs/probes.md
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,20 @@ The probe returns a single OutcomeNotApplicable if the projects has had no pull
The probe returns 1 true outcome if the project has no workflows "write" permissions a the "top" level.


## unsafeblock

**Lifecycle**: experimental

**Description**: Flags unsafe blocks of code in this project.

**Motivation**: Memory safety in software should be considered a continuum, rather than being binary. While some languages and tools are memory safe by default, it may still be possible, and sometimes unavoidable, to write unsafe code in them. Unsafe code allow developers to bypass normal safety checks and directly manipulate memory.

**Implementation**: The probe is ecosystem-specific and will surface non memory safe practices in the project by identifying unsafe code blocks. Unsafe code blocks are supported in rust, go, c#, and swift, but only go and c# are supported by this probe at this time: - for go the probe will look for the use of the `unsafe` include directive. - for c# the probe will look at the csproj and identify the use of the `AllowUnsafeBlocks` property.

**Outcomes**: For supported ecosystem, the probe returns OutcomeTrue per unsafe block.
If the project has no unsafe blocks, the probe returns OutcomeFalse.


## webhooksUseSecrets

**Lifecycle**: experimental
Expand Down
15 changes: 14 additions & 1 deletion internal/dotnet/csproj/csproj.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ var errInvalidCsProjFile = errors.New("error parsing csproj file")
type PropertyGroup struct {
XMLName xml.Name `xml:"PropertyGroup"`
RestoreLockedMode bool `xml:"RestoreLockedMode"`
AllowUnsafeBlocks bool `xml:"AllowUnsafeBlocks"`
}

type Project struct {
Expand All @@ -32,6 +33,18 @@ type Project struct {
}

func IsRestoreLockedModeEnabled(content []byte) (bool, error) {
return isCsProjFilePropertyGroupEnabled(content, func(propertyGroup *PropertyGroup) bool {
return propertyGroup.RestoreLockedMode
})
}

func IsAllowUnsafeBlocksEnabled(content []byte) (bool, error) {
return isCsProjFilePropertyGroupEnabled(content, func(propertyGroup *PropertyGroup) bool {
return propertyGroup.AllowUnsafeBlocks
})
}

func isCsProjFilePropertyGroupEnabled(content []byte, predicate func(*PropertyGroup) bool) (bool, error) {
var project Project

err := xml.Unmarshal(content, &project)
Expand All @@ -40,7 +53,7 @@ func IsRestoreLockedModeEnabled(content []byte) (bool, error) {
}

for _, propertyGroup := range project.PropertyGroups {
if propertyGroup.RestoreLockedMode {
if predicate(&propertyGroup) {
return true, nil
}
}
Expand Down
5 changes: 4 additions & 1 deletion probes/entries.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ import (
"github.com/ossf/scorecard/v5/probes/securityPolicyPresent"
"github.com/ossf/scorecard/v5/probes/testsRunInCI"
"github.com/ossf/scorecard/v5/probes/topLevelPermissions"
"github.com/ossf/scorecard/v5/probes/unsafeblock"
"github.com/ossf/scorecard/v5/probes/webhooksUseSecrets"
)

Expand Down Expand Up @@ -173,7 +174,9 @@ var (
}

// Probes which don't use pre-computed raw data but rather collect it themselves.
Independent = []IndependentProbeImpl{}
Independent = []IndependentProbeImpl{
unsafeblock.Run,
}
)

//nolint:gochecknoinits
Expand Down
44 changes: 44 additions & 0 deletions probes/unsafeblock/def.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Copyright 2025 OpenSSF Scorecard Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

id: unsafeblock
lifecycle: experimental
short: Flags unsafe blocks of code in this project.
motivation: >
Memory safety in software should be considered a continuum, rather than being binary.
While some languages and tools are memory safe by default, it may still be possible, and sometimes unavoidable, to write unsafe code in them.
Unsafe code allow developers to bypass normal safety checks and directly manipulate memory.
implementation: >
The probe is ecosystem-specific and will surface non memory safe practices in the project by identifying unsafe code blocks.
Unsafe code blocks are supported in rust, go, c#, and swift, but only go and c# are supported by this probe at this time:
- for go the probe will look for the use of the `unsafe` include directive.
- for c# the probe will look at the csproj and identify the use of the `AllowUnsafeBlocks` property.
outcome:
- For supported ecosystem, the probe returns OutcomeTrue per unsafe block.
- If the project has no unsafe blocks, the probe returns OutcomeFalse.
remediation:
onOutcome: True
effort: Medium
text:
- Visit the OpenSSF Memory Safety SIG guidance on how to make your project memory safe.
- Guidance for [Memory-Safe By Default Languages](https://github.com/ossf/Memory-Safety/blob/main/docs/best-practice-memory-safe-by-default-languages.md)
- Guidance for [Non Memory-Safe By Default Languages](https://github.com/ossf/Memory-Safety/blob/main/docs/best-practice-non-memory-safe-by-default-languages.md)
ecosystem:
languages:
- go
- c#
clients:
- github
- gitlab
- localdir
210 changes: 210 additions & 0 deletions probes/unsafeblock/impl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
// Copyright 2025 OpenSSF Scorecard Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package unsafeblock

import (
"embed"
"fmt"
"go/parser"
"go/token"
"reflect"
"strings"

"github.com/ossf/scorecard/v5/checker"
"github.com/ossf/scorecard/v5/checks/fileparser"
"github.com/ossf/scorecard/v5/clients"
"github.com/ossf/scorecard/v5/finding"
"github.com/ossf/scorecard/v5/internal/dotnet/csproj"
"github.com/ossf/scorecard/v5/internal/probes"
)

//go:embed *.yml
var fs embed.FS

const (
Probe = "unsafeblock"
)

type languageMemoryCheckConfig struct {
funcPointer func(client *checker.CheckRequest) ([]finding.Finding, error)
Desc string
}

var languageMemorySafeSpecs = map[clients.LanguageName]languageMemoryCheckConfig{
clients.Go: {
funcPointer: checkGoUnsafePackage,
Desc: "Check if Go code uses the unsafe package",
},

clients.CSharp: {
funcPointer: checkDotnetAllowUnsafeBlocks,
Desc: "Check if C# code uses unsafe blocks",
},
}

func init() {
probes.MustRegisterIndependent(Probe, Run)
}

func Run(raw *checker.CheckRequest) (found []finding.Finding, probeName string, err error) {
repoLanguageChecks, err := getLanguageChecks(raw)
if err != nil {
return nil, Probe, err
}
findings := []finding.Finding{}
for _, lang := range repoLanguageChecks {
langFindings, err := lang.funcPointer(raw)
if err != nil {
return nil, Probe, fmt.Errorf("error while running function for language %s: %w", lang.Desc, err)
}
findings = append(findings, langFindings...)
}
var nonErrorFindings bool
for _, f := range findings {
if f.Outcome != finding.OutcomeError {
nonErrorFindings = true
}
}
// if we don't have any findings (ignoring OutcomeError), we think it's safe
if !nonErrorFindings {
found, err := finding.NewWith(fs, Probe,
"All supported ecosystems do not declare or use unsafe code blocks", nil, finding.OutcomeFalse)
if err != nil {
return nil, Probe, fmt.Errorf("create finding: %w", err)
}

Check warning on line 86 in probes/unsafeblock/impl.go

View check run for this annotation

Codecov / codecov/patch

probes/unsafeblock/impl.go#L85-L86

Added lines #L85 - L86 were not covered by tests
findings = append(findings, *found)
}
return findings, Probe, nil
}

func getLanguageChecks(raw *checker.CheckRequest) ([]languageMemoryCheckConfig, error) {
langs, err := raw.RepoClient.ListProgrammingLanguages()
if err != nil {
return nil, fmt.Errorf("cannot get langs of repo: %w", err)
}
if len(langs) == 1 && langs[0].Name == clients.All {
return getAllLanguages(), nil
}
ret := []languageMemoryCheckConfig{}
for _, language := range langs {
if lang, ok := languageMemorySafeSpecs[clients.LanguageName(strings.ToLower(string(language.Name)))]; ok {
ret = append(ret, lang)
}
}
return ret, nil
}

func getAllLanguages() []languageMemoryCheckConfig {
allLanguages := make([]languageMemoryCheckConfig, 0, len(languageMemorySafeSpecs))
for l := range languageMemorySafeSpecs {
allLanguages = append(allLanguages, languageMemorySafeSpecs[l])
}
return allLanguages
}

// Golang

func checkGoUnsafePackage(client *checker.CheckRequest) ([]finding.Finding, error) {
findings := []finding.Finding{}
if err := fileparser.OnMatchingFileContentDo(client.RepoClient, fileparser.PathMatcher{
Pattern: "*.go",
CaseSensitive: false,
}, goCodeUsesUnsafePackage, &findings); err != nil {
return nil, err
}

return findings, nil
}

func goCodeUsesUnsafePackage(path string, content []byte, args ...interface{}) (bool, error) {
findings, ok := args[0].(*[]finding.Finding)
if !ok {
// panic if it is not correct type
panic(fmt.Sprintf("expected type findings, got %v", reflect.TypeOf(args[0])))

Check warning on line 135 in probes/unsafeblock/impl.go

View check run for this annotation

Codecov / codecov/patch

probes/unsafeblock/impl.go#L134-L135

Added lines #L134 - L135 were not covered by tests
}
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, "", content, parser.ImportsOnly)
if err != nil {
found, err := finding.NewWith(fs, Probe, "malformed golang file", &finding.Location{
Path: path,
}, finding.OutcomeError)
if err != nil {
return false, fmt.Errorf("create finding: %w", err)
}

Check warning on line 145 in probes/unsafeblock/impl.go

View check run for this annotation

Codecov / codecov/patch

probes/unsafeblock/impl.go#L144-L145

Added lines #L144 - L145 were not covered by tests
*findings = append(*findings, *found)
return true, nil
}
for _, i := range f.Imports {
if i.Path.Value == `"unsafe"` {
lineStart := uint(fset.Position(i.Pos()).Line)
found, err := finding.NewWith(fs, Probe,
"Golang code uses the unsafe package", &finding.Location{
Path: path, LineStart: &lineStart,
}, finding.OutcomeTrue)
if err != nil {
return false, fmt.Errorf("create finding: %w", err)
}

Check warning on line 158 in probes/unsafeblock/impl.go

View check run for this annotation

Codecov / codecov/patch

probes/unsafeblock/impl.go#L157-L158

Added lines #L157 - L158 were not covered by tests
*findings = append(*findings, *found)
}
}

return true, nil
}

// CSharp

func checkDotnetAllowUnsafeBlocks(client *checker.CheckRequest) ([]finding.Finding, error) {
findings := []finding.Finding{}

if err := fileparser.OnMatchingFileContentDo(client.RepoClient, fileparser.PathMatcher{
Pattern: "*.csproj",
CaseSensitive: false,
}, csProjAllosUnsafeBlocks, &findings); err != nil {
return nil, err
}
return findings, nil
}

func csProjAllosUnsafeBlocks(path string, content []byte, args ...interface{}) (bool, error) {
findings, ok := args[0].(*[]finding.Finding)
if !ok {
// panic if it is not correct type
panic(fmt.Sprintf("expected type findings, got %v", reflect.TypeOf(args[0])))

Check warning on line 184 in probes/unsafeblock/impl.go

View check run for this annotation

Codecov / codecov/patch

probes/unsafeblock/impl.go#L183-L184

Added lines #L183 - L184 were not covered by tests
}

unsafe, err := csproj.IsAllowUnsafeBlocksEnabled(content)
if err != nil {
found, err := finding.NewWith(fs, Probe, "malformed csproj file", &finding.Location{
Path: path,
}, finding.OutcomeError)
if err != nil {
return false, fmt.Errorf("create finding: %w", err)
}

Check warning on line 194 in probes/unsafeblock/impl.go

View check run for this annotation

Codecov / codecov/patch

probes/unsafeblock/impl.go#L193-L194

Added lines #L193 - L194 were not covered by tests
*findings = append(*findings, *found)
return true, nil
}
if unsafe {
found, err := finding.NewWith(fs, Probe,
"C# project file allows the use of unsafe blocks", &finding.Location{
Path: path,
}, finding.OutcomeTrue)
if err != nil {
return false, fmt.Errorf("create finding: %w", err)
}

Check warning on line 205 in probes/unsafeblock/impl.go

View check run for this annotation

Codecov / codecov/patch

probes/unsafeblock/impl.go#L204-L205

Added lines #L204 - L205 were not covered by tests
*findings = append(*findings, *found)
}

return true, nil
}
Loading
Loading