aboutsummaryrefslogtreecommitdiffstats
path: root/pkg/vcs/linux_patches.go
blob: 224e934048b3e02977704c3254a11c2faebcf83e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Copyright 2023 syzkaller project authors. All rights reserved.
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.

package vcs

import "fmt"

// BackportCommit describes a fix commit that must be cherry-picked to an older
// kernel revision in order to enable kernel build / boot.
type BackportCommit struct {
	// Backport is only applied if the commit is reachable from HEAD.
	GuiltyHash string `json:"guilty_hash"`
	// The hash of the commit to cherry-pick.
	FixHash string `json:"fix_hash"`
	// The title of the commit to cherry-pick.
	// It's used to determine whether the fix is already in place.
	FixTitle string `json:"fix_title"`
	// The field is only intended to make config files less cryptic.
	Comment string `json:"comment"`
}

// linuxFixBackports() cherry-picks the commits necessary to compile/run older Linux kernel releases.
func linuxFixBackports(repo *gitRepo, extraCommits ...BackportCommit) error {
	return applyFixBackports(repo,
		append(
			append([]BackportCommit{}, pickLinuxCommits...),
			extraCommits...,
		),
	)
}

func applyFixBackports(repo *gitRepo, commits []BackportCommit) error {
	for _, info := range commits {
		if info.GuiltyHash != "" {
			contains, err := repo.Contains(info.GuiltyHash)
			if err != nil {
				return fmt.Errorf("failed to check if %s is present: %w", info.GuiltyHash, err)
			}
			if !contains {
				// There's no reason to backport a fix.
				continue
			}
		}
		fixCommit, err := repo.GetCommitByTitle(info.FixTitle)
		if err != nil {
			return err
		}
		if fixCommit != nil {
			// The fix is already present.
			continue
		}
		_, err = repo.Run("cherry-pick", "--no-commit", info.FixHash)
		if err != nil {
			return err
		}
	}
	return nil
}

var pickLinuxCommits = []BackportCommit{
	{
		// Compiling v4.6..v5.11 with a modern objtool, w/o this patch, results in the
		// following issue, when compiling with clang:
		// arch/x86/entry/thunk_64.o: warning: objtool: missing symbol table
		// We don't bisect that far back with neither clang nor gcc, so this should be fine:
		FixHash:  `1d489151e9f9d1647110277ff77282fe4d96d09b`,
		FixTitle: `objtool: Don't fail on missing symbol table`,
	},
	{
		// With newer compiler versions, kernel compilation fails with:
		// subcmd-util.h:56:23: error: pointer may be used after ‘realloc’ [-Werror=use-after-free]
		// 56 |                 ret = realloc(ptr, size);
		// The guilty commit is from 2015, we don't bisect that far.
		FixHash:  `52a9dab6d892763b2a8334a568bd4e2c1a6fde66`,
		FixTitle: `libsubcmd: Fix use-after-free for realloc(..., 0)`,
	},
	{
		// A number of old releases fail with KASAN: use-after-free in task_active_pid_ns.
		// The problem was actually present so long ago that we do not need to check whether
		// the guilty commit is present. We don't bisect that back (v2.*) anyway.
		FixHash:  `0711f0d7050b9e07c44bc159bbc64ac0a1022c7f`,
		FixTitle: "pid: take a reference when initializing `cad_pid`",
	},
	{
		// Fixes the following error:
		// check.c:2865:58: error: '%d' directive output may be truncated writing between 1 and
		// 10 bytes into a region of size 9 [-Werror=format-truncation=]
		GuiltyHash: `db2b0c5d7b6f19b3c2cab08c531b65342eb5252b`,
		FixHash:    `82880283d7fcd0a1d20964a56d6d1a5cc0df0713`,
		FixTitle:   `objtool: Fix truncated string warning`,
	},
	{
		// Fixes `boot failed: WARNING in kvm_wait`.
		GuiltyHash: `997acaf6b4b59c6a9c259740312a69ea549cc684`,
		FixHash:    `f4e61f0c9add3b00bd5f2df3c814d688849b8707`,
		FixTitle:   `x86/kvm: Fix broken irq restoration in kvm_wait`,
	},
	{
		// Fixes `error: implicit declaration of function 'acpi_mps_check'`.
		GuiltyHash: `342f43af70dbc74f8629381998f92c060e1763a2`,
		FixHash:    `ea7b4244b3656ca33b19a950f092b5bbc718b40c`,
		FixTitle:   `x86/setup: Explicitly include acpi.h`,
	},
	{
		// Fixes `BUG: KASAN: slab-use-after-free in binder_add_device` at boot.
		GuiltyHash: `12d909cac1e1c4147cc3417fee804ee12fc6b984`,
		FixHash:    `e77aff5528a183462714f750e45add6cc71e276a`,
		FixTitle:   `binderfs: fix use-after-free in binder_devices`,
	},
}