Source file src/cmd/internal/buildid/rewrite.go

     1  // Copyright 2017 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package buildid
     6  
     7  import (
     8  	"bytes"
     9  	"cmd/internal/codesign"
    10  	imacho "cmd/internal/macho"
    11  	"crypto/sha256"
    12  	"debug/elf"
    13  	"debug/macho"
    14  	"fmt"
    15  	"io"
    16  )
    17  
    18  // FindAndHash reads all of r and returns the offsets of occurrences of id.
    19  // While reading, findAndHash also computes and returns
    20  // a hash of the content of r, but with occurrences of id replaced by zeros.
    21  // FindAndHash reads bufSize bytes from r at a time.
    22  // If bufSize == 0, FindAndHash uses a reasonable default.
    23  func FindAndHash(r io.Reader, id string, bufSize int) (matches []int64, hash [32]byte, err error) {
    24  	if bufSize == 0 {
    25  		bufSize = 31 * 1024 // bufSize+little will likely fit in 32 kB
    26  	}
    27  	if len(id) == 0 {
    28  		return nil, [32]byte{}, fmt.Errorf("buildid.FindAndHash: no id specified")
    29  	}
    30  	if len(id) > bufSize {
    31  		return nil, [32]byte{}, fmt.Errorf("buildid.FindAndHash: buffer too small")
    32  	}
    33  	zeros := make([]byte, len(id))
    34  	idBytes := []byte(id)
    35  
    36  	r0 := r // preserve original type of r
    37  
    38  	// For Mach-O files, we want to exclude the code signature.
    39  	// The code signature contains hashes of the whole file (except the signature
    40  	// itself), including the buildid. So the buildid cannot contain the signature.
    41  	r = excludeMachoCodeSignature(r)
    42  
    43  	// With the "-B gobuildid" linker option (which will be the default on some
    44  	// platforms), the host build ID (GNU build ID, Mach-O UUID) depends on the
    45  	// Go buildid. So ignore the host build ID, to avoid convergence problem.
    46  	r = excludeHostBuildID(r, r0)
    47  
    48  	// The strategy is to read the file through buf, looking for id,
    49  	// but we need to worry about what happens if id is broken up
    50  	// and returned in parts by two different reads.
    51  	// We allocate a tiny buffer (at least len(id)) and a big buffer (bufSize bytes)
    52  	// next to each other in memory and then copy the tail of
    53  	// one read into the tiny buffer before reading new data into the big buffer.
    54  	// The search for id is over the entire tiny+big buffer.
    55  	tiny := (len(id) + 127) &^ 127 // round up to 128-aligned
    56  	buf := make([]byte, tiny+bufSize)
    57  	h := sha256.New()
    58  	start := tiny
    59  	for offset := int64(0); ; {
    60  		// The file offset maintained by the loop corresponds to &buf[tiny].
    61  		// buf[start:tiny] is left over from previous iteration.
    62  		// After reading n bytes into buf[tiny:], we process buf[start:tiny+n].
    63  		n, err := io.ReadFull(r, buf[tiny:])
    64  		if err != io.ErrUnexpectedEOF && err != io.EOF && err != nil {
    65  			return nil, [32]byte{}, err
    66  		}
    67  
    68  		// Process any matches.
    69  		for {
    70  			i := bytes.Index(buf[start:tiny+n], idBytes)
    71  			if i < 0 {
    72  				break
    73  			}
    74  			matches = append(matches, offset+int64(start+i-tiny))
    75  			h.Write(buf[start : start+i])
    76  			h.Write(zeros)
    77  			start += i + len(id)
    78  		}
    79  		if n < bufSize {
    80  			// Did not fill buffer, must be at end of file.
    81  			h.Write(buf[start : tiny+n])
    82  			break
    83  		}
    84  
    85  		// Process all but final tiny bytes of buf (bufSize = len(buf)-tiny).
    86  		// Note that start > len(buf)-tiny is possible, if the search above
    87  		// found an id ending in the final tiny fringe. That's OK.
    88  		if start < len(buf)-tiny {
    89  			h.Write(buf[start : len(buf)-tiny])
    90  			start = len(buf) - tiny
    91  		}
    92  
    93  		// Slide ending tiny-sized fringe to beginning of buffer.
    94  		copy(buf[0:], buf[bufSize:])
    95  		start -= bufSize
    96  		offset += int64(bufSize)
    97  	}
    98  	h.Sum(hash[:0])
    99  	return matches, hash, nil
   100  }
   101  
   102  func Rewrite(w io.WriterAt, pos []int64, id string) error {
   103  	b := []byte(id)
   104  	for _, p := range pos {
   105  		if _, err := w.WriteAt(b, p); err != nil {
   106  			return err
   107  		}
   108  	}
   109  
   110  	// Update Mach-O code signature, if any.
   111  	if f, cmd, ok := findMachoCodeSignature(w); ok {
   112  		if codesign.Size(int64(cmd.Dataoff), "a.out") == int64(cmd.Datasize) {
   113  			// Update the signature if the size matches, so we don't need to
   114  			// fix up headers. Binaries generated by the Go linker should have
   115  			// the expected size. Otherwise skip.
   116  			text := f.Segment("__TEXT")
   117  			cs := make([]byte, cmd.Datasize)
   118  			codesign.Sign(cs, w.(io.Reader), "a.out", int64(cmd.Dataoff), int64(text.Offset), int64(text.Filesz), f.Type == macho.TypeExec)
   119  			if _, err := w.WriteAt(cs, int64(cmd.Dataoff)); err != nil {
   120  				return err
   121  			}
   122  		}
   123  	}
   124  
   125  	return nil
   126  }
   127  
   128  func excludeMachoCodeSignature(r io.Reader) io.Reader {
   129  	_, cmd, ok := findMachoCodeSignature(r)
   130  	if !ok {
   131  		return r
   132  	}
   133  	return &excludedReader{r, 0, int64(cmd.Dataoff), int64(cmd.Dataoff + cmd.Datasize)}
   134  }
   135  
   136  func excludeHostBuildID(r, r0 io.Reader) io.Reader {
   137  	off, sz, ok := findHostBuildID(r0)
   138  	if !ok {
   139  		return r
   140  	}
   141  	return &excludedReader{r, 0, off, off + sz}
   142  }
   143  
   144  // excludedReader wraps an io.Reader. Reading from it returns the bytes from
   145  // the underlying reader, except that when the byte offset is within the
   146  // range between start and end, it returns zero bytes.
   147  type excludedReader struct {
   148  	r          io.Reader
   149  	off        int64 // current offset
   150  	start, end int64 // the range to be excluded (read as zero)
   151  }
   152  
   153  func (r *excludedReader) Read(p []byte) (int, error) {
   154  	n, err := r.r.Read(p)
   155  	if n > 0 && r.off+int64(n) > r.start && r.off < r.end {
   156  		cstart := r.start - r.off
   157  		if cstart < 0 {
   158  			cstart = 0
   159  		}
   160  		cend := r.end - r.off
   161  		if cend > int64(n) {
   162  			cend = int64(n)
   163  		}
   164  		zeros := make([]byte, cend-cstart)
   165  		copy(p[cstart:cend], zeros)
   166  	}
   167  	r.off += int64(n)
   168  	return n, err
   169  }
   170  
   171  func findMachoCodeSignature(r any) (*macho.File, codesign.CodeSigCmd, bool) {
   172  	ra, ok := r.(io.ReaderAt)
   173  	if !ok {
   174  		return nil, codesign.CodeSigCmd{}, false
   175  	}
   176  	f, err := macho.NewFile(ra)
   177  	if err != nil {
   178  		return nil, codesign.CodeSigCmd{}, false
   179  	}
   180  	cmd, ok := codesign.FindCodeSigCmd(f)
   181  	return f, cmd, ok
   182  }
   183  
   184  func findHostBuildID(r io.Reader) (offset int64, size int64, ok bool) {
   185  	ra, ok := r.(io.ReaderAt)
   186  	if !ok {
   187  		return 0, 0, false
   188  	}
   189  
   190  	ef, err := elf.NewFile(ra)
   191  	if err == nil {
   192  		// ELF file. Find GNU build ID section.
   193  		sect := ef.Section(".note.gnu.build-id")
   194  		if sect == nil {
   195  			return 0, 0, false
   196  		}
   197  		// Skip over the 3-word note "header" and "GNU\x00".
   198  		return int64(sect.Offset + 16), int64(sect.Size - 16), true
   199  	}
   200  
   201  	mf, err := macho.NewFile(ra)
   202  	if err != nil {
   203  		return 0, 0, false
   204  	}
   205  
   206  	// Mach-O file. Find LC_UUID load command.
   207  	reader := imacho.NewLoadCmdReader(io.NewSectionReader(ra, 0, 1<<63-1), mf.ByteOrder, imacho.FileHeaderSize(mf))
   208  	for i := uint32(0); i < mf.Ncmd; i++ {
   209  		cmd, err := reader.Next()
   210  		if err != nil {
   211  			break
   212  		}
   213  		if cmd.Cmd == imacho.LC_UUID {
   214  			// The UUID is the data in the LC_UUID load command,
   215  			// skipping over the 8-byte command header.
   216  			return int64(reader.Offset() + 8), int64(cmd.Len - 8), true
   217  		}
   218  	}
   219  	return 0, 0, false
   220  }
   221  

View as plain text