xref: /aosp_15_r20/external/boringssl/src/util/check_stack.go (revision 8fb009dc861624b67b6cdb62ea21f0f22d0c584b)
1// Copyright (c) 2022, Google Inc.
2//
3// Permission to use, copy, modify, and/or distribute this software for any
4// purpose with or without fee is hereby granted, provided that the above
5// copyright notice and this permission notice appear in all copies.
6//
7// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14
15//go:build ignore
16
17// check_stack.go checks that each of its arguments has a non-executable stack.
18// See https://www.airs.com/blog/archives/518 for details.
19package main
20
21import (
22	"debug/elf"
23	"fmt"
24	"os"
25)
26
27func checkStack(path string) {
28	file, err := elf.Open(path)
29	if err != nil {
30		fmt.Fprintf(os.Stderr, "Error opening %s: %s\n", path, err)
31		os.Exit(1)
32	}
33	defer file.Close()
34
35	for _, prog := range file.Progs {
36		if prog.Type == elf.PT_GNU_STACK && prog.Flags&elf.PF_X != 0 {
37			fmt.Fprintf(os.Stderr, "%s has an executable stack.\n", path)
38			os.Exit(1)
39		}
40	}
41}
42
43func main() {
44	for _, path := range os.Args[1:] {
45		checkStack(path)
46	}
47}
48