summaryrefslogtreecommitdiff
path: root/prelink.c
blob: 07516f8749815bb093bfc47161963b291c6d1c4e (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#define _XOPEN_SOURCE 500

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <fcntl.h>

static inline int
elf16(unsigned char *buf, int le)
{
  if (le)
    return buf[0] | buf[1] << 8;
  return buf[0] << 8 | buf[1];
}

static inline unsigned int
elf32(unsigned char *buf, int le)
{
  if (le)
    return buf[0] | buf[1] << 8 | buf[2] << 16 | buf[3] << 24;
  return buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
}

static inline unsigned int
elf64(unsigned char *buf, int le, int is64)
{
  if (is64)
    {
      buf += le ? 4 : 0;
      if (buf[0] || buf[1] || buf[2] || buf[3])
	return ~0;
      buf += le ? -4 : 4;
    }
  if (le)
    return buf[0] | buf[1] << 8 | buf[2] << 16 | buf[3] << 24;
  return buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
}

int
is_prelinked(int fd, unsigned char *buf, int l)
{
  int le, is64;
  off_t soff;
  int snum, ssiz;
  int i, stridx;
  unsigned char *sects, *strsect;
  unsigned int slen;
  unsigned int o;

  if (l < 0x34)
    return 0;
  if (buf[0] != 0x7f || buf[1] != 'E' || buf[2] != 'L' || buf[3] != 'F')
    return 0;
  is64 = buf[4] == 2;
  le = buf[5] != 2;
  if (is64 && l < 0x40)
    return 0;
  soff = elf64(is64 ? buf + 40 : buf + 32, le, is64);
  if (soff == (off_t)~0)
    return 0;
  ssiz = elf16(buf + (is64 ? 0x40 - 6 : 0x34 - 6), le);
  if (ssiz < (is64 ? 64 : 40) || ssiz >= 32768)
    return 0;
  snum = elf16(buf + (is64 ? 0x40 - 4 : 0x34 - 4), le);
  stridx = elf16(buf + (is64 ? 0x40 - 2 : 0x34 - 2), le);
  if (stridx >= snum)
    return 0;
  sects = malloc(snum * ssiz);
  if (!sects)
    return 0;
  if (pread(fd, sects, snum * ssiz, soff) != snum * ssiz)
    {
      free(sects);
      return 0;
    }
  strsect = sects + stridx * ssiz;
  if (elf32(strsect + 4, le) != 3)
    {
      free(sects);
      return 0;
    }
  soff = elf64(is64 ? strsect + 24 : strsect + 16, le, is64);
  slen = elf64(is64 ? strsect + 32 : strsect + 20, le, is64);
  if (soff == (off_t)~0 || slen == ~0 || (int)slen < 0)
    {
      free(sects);
      return 0;
    }
  strsect = malloc(slen);
  if (!strsect)
    {
      free(sects);
      return 0;
    }
  if (pread(fd, strsect, slen, soff) != slen)
    {
      free(sects);
      free(strsect);
      return 0;
    }
  for (i = 0; i < snum; i++)
    {
      o = elf32(sects + i * ssiz, le);
      if (o > slen)
	continue;
      /* printf("sect #%d %s\n", i, strsect + o); */
      if (o + 18 <= slen && memcmp(strsect + o, ".gnu.prelink_undo", 18) == 0)
	break;
    }
  free(strsect);
  free(sects);
  return i == snum ? 0 : 1;
}


pid_t prelink_pid;

int
prelinked_open(char *name)
{
  pid_t pid;
  int fd, status;
  struct stat stb;
  char template[21];

  if (stat("/usr/sbin/prelink", &stb))
    {
      perror("/usr/sbin/prelink");
      fprintf(stderr, "prelink not installed, cannot undo prelinking");
      exit(1);
    }
  strcpy(template, "/tmp/deltarpm.XXXXXX");
  if ((fd = mkstemp(template)) == -1)
    {
      perror("mkstemp");
      exit(1);
    }
  close(fd);    /* prelink renames another tmpfile over our file */
  pid = fork();
  if (pid == (pid_t)(-1))
    {
      perror("fork");
      exit(1);
    }
  if (!pid)
    {
      execl("/usr/sbin/prelink", "prelink", "-o", template, "-u", name, (char *)0);
      perror("/usr/sbin/prelink");
      _exit(1);
    }
  while (waitpid(pid, &status, 0) == (pid_t)-1)
    ;
  if ((fd = open(template, O_RDONLY)) == -1)
    {
      perror(template);
      exit(1);
    }
  unlink(template);
  return fd; 
}