summaryrefslogtreecommitdiffstats
path: root/security/nss/cmd/shlibsign/mangle/mangle.c
blob: e58bbee410e3ec50c679785f6e8ab15d22235c70 (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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/*
 * Test program to mangle 1 bit in a binary
 */

#include "nspr.h"
#include "plstr.h"
#include "plgetopt.h"
#include "prio.h"

static PRFileDesc *pr_stderr;
static void
usage(char *program_name)
{

    PR_fprintf(pr_stderr, "Usage:");
    PR_fprintf(pr_stderr, "%s -i shared_library_name -o byte_offset -b bit\n", program_name);
}

int
main(int argc, char **argv)
{
    /* buffers and locals */
    PLOptState *optstate;
    char *programName;
    char cbuf;

    /* parameter set variables */
    const char *libFile = NULL;
    int bitOffset = -1;

    /* return values */
    int retval = 2; /* 0 - test succeeded.
                     * 1 - illegal args
                     * 2 - function failed */
    PRFileDesc *fd = NULL;
    int bytesRead;
    int bytesWritten;
    PROffset32 offset = -1;
    PROffset32 pos;

    programName = PL_strrchr(argv[0], '/');
    programName = programName ? (programName + 1) : argv[0];

    pr_stderr = PR_STDERR;

    optstate = PL_CreateOptState(argc, argv, "i:o:b:");
    if (optstate == NULL) {
        return 1;
    }

    while (PL_GetNextOpt(optstate) == PL_OPT_OK) {
        switch (optstate->option) {
            case 'i':
                libFile = optstate->value;
                break;

            case 'o':
                offset = atoi(optstate->value);
                break;

            case 'b':
                bitOffset = atoi(optstate->value);
                break;
        }
    }

    if (libFile == NULL) {
        usage(programName);
        return 1;
    }
    if ((bitOffset >= 8) || (bitOffset < 0)) {
        usage(programName);
        return 1;
    }

    /* open the target signature file */
    fd = PR_OpenFile(libFile, PR_RDWR, 0666);
    if (fd == NULL) {
        /* lperror(libFile); */
        PR_fprintf(pr_stderr, "Couldn't Open %s\n", libFile);
        goto loser;
    }

    if (offset < 0) { /* convert to positive offset */
        pos = PR_Seek(fd, offset, PR_SEEK_END);
        if (pos == -1) {
            PR_fprintf(pr_stderr, "Seek for read on %s (to %d) failed\n",
                       libFile, offset);
            goto loser;
        }
        offset = pos;
    }

    /* read the byte */
    pos = PR_Seek(fd, offset, PR_SEEK_SET);
    if (pos != offset) {
        PR_fprintf(pr_stderr, "Seek for read on %s (to %d) failed\n",
                   libFile, offset);
        goto loser;
    }
    bytesRead = PR_Read(fd, &cbuf, 1);
    if (bytesRead != 1) {
        PR_fprintf(pr_stderr, "Read on %s (to %d) failed\n", libFile, offset);
        goto loser;
    }

    PR_fprintf(pr_stderr, "Changing byte 0x%08x (%d): from %02x (%d) to ",
               offset, offset, (unsigned char)cbuf, (unsigned char)cbuf);
    /* change it */
    cbuf ^= 1 << bitOffset;
    PR_fprintf(pr_stderr, "%02x (%d)\n",
               (unsigned char)cbuf, (unsigned char)cbuf);

    /* write it back out */
    pos = PR_Seek(fd, offset, PR_SEEK_SET);
    if (pos != offset) {
        PR_fprintf(pr_stderr, "Seek for write on %s (to %d) failed\n",
                   libFile, offset);
        goto loser;
    }
    bytesWritten = PR_Write(fd, &cbuf, 1);
    if (bytesWritten != 1) {
        PR_fprintf(pr_stderr, "Write on %s (to %d) failed\n", libFile, offset);
        goto loser;
    }

    retval = 0;

loser:
    if (fd)
        PR_Close(fd);
    PR_Cleanup();
    return retval;
}

/*#DEFINES += -DSHLIB_SUFFIX=\"$(DLL_SUFFIX)\" -DSHLIB_PREFIX=\"$(DLL_PREFIX)\" */