File: test.c

package info (click to toggle)
ecryptfs-utils 111-6
  • links: PTS
  • area: main
  • in suites: bookworm
  • size: 3,528 kB
  • sloc: ansic: 18,280; sh: 7,043; makefile: 350; python: 41
file content (160 lines) | stat: -rw-r--r-- 3,681 bytes parent folder | download | duplicates (7)
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
/*
 * Author: Colin King <colin.king@canonical.com>
 *
 * Copyright (C) 2012 Canonical, Ltd.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 */

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/klog.h>
#include <limits.h>

#define TEST_PASSED	(0)
#define TEST_FAILED	(1)
#define TEST_ERROR	(2)

#define MMAP_LEN	(8192)

/* read kernel log */
char *klog_read(void)
{
	int len;
	char *klog;

	if ((len = klogctl(10, NULL, 0)) < 0)
		return NULL;
	
	if ((klog = calloc(1, len)) == NULL)
		return NULL;

	if (klogctl(3, klog, len) < 0) {
		free(klog);
		return NULL;
	}
	return klog;
}

/*
 *  https://bugs.launchpad.net/ubuntu/+bug/870326
 *	open(), mmap(), close() and write to mapping causes
 * 	an error.
 */
int main(int argc, char **argv)
{
	int fd;
	int rc = TEST_PASSED;
	unsigned int *ptr;
	char buffer[MMAP_LEN];
	char *klog_before;
	char *klog_after, *klog_new_text;
	size_t n, lastline_len = 0;

	if (argc < 2) {
		fprintf(stderr, "Usage: file\n");
		exit(TEST_ERROR);
	}

	if ((fd = open(argv[1], O_CREAT | O_RDWR, S_IRUSR | S_IWUSR)) < 0) {
		fprintf(stderr, "Cannot create %s : %s\n",
			argv[1], strerror(errno));
		exit(TEST_ERROR);
	}

	memset(buffer, 'X', sizeof(buffer));
	if (write(fd, buffer, sizeof(buffer)) < 0) {
		fprintf(stderr, "Failed to write to %s : %s\n",
			argv[1], strerror(errno));
		close(fd);
		rc = TEST_ERROR;
		goto tidy;
	}

	ptr = mmap(NULL, MMAP_LEN, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
	if (ptr == MAP_FAILED) {
		fprintf(stderr, "Cannot mmap file %s : %s\n",
			argv[1], strerror(errno));
		close(fd);
		rc = TEST_ERROR;
		goto tidy;
	}
	
	if (close(fd) < 0) {
		fprintf(stderr, "Failed to close : %s\n", strerror(errno));
		munmap(ptr, MMAP_LEN);
		rc = TEST_ERROR;
		goto tidy;
	}

	if ((klog_before = klog_read()) == NULL) {
		fprintf(stderr, "Failed to read kernel log\n");
		munmap(ptr, MMAP_LEN);
		rc = TEST_ERROR;
		goto tidy;
	}

	/* Seek back to start of last line */
	if ((n = strlen(klog_before)) > 0) {
		n--;
		lastline_len++;
		while (n > 0 && klog_before[n-1] != '\n') {
			n--;
			lastline_len++;
		}
	}

	/* Modify pages, this caused bug LP#870326 */
	memset(ptr, ' ', MMAP_LEN);

	if (munmap(ptr, MMAP_LEN) < 0) {
		fprintf(stderr, "munmap failed : %s\n", strerror(errno));
		free(klog_before);
		rc = TEST_ERROR;
		goto tidy;
	}
	
	/*
	 * Get klog again, find previous klog last line
	 * and offset to end of this
	 */
	if ((klog_after = klog_read()) == NULL) {
		fprintf(stderr, "Failed to read kernel log\n");
		free(klog_before);
		rc = TEST_ERROR;
		goto tidy;
	}
	klog_new_text = strstr(klog_after, klog_before + n);

	/* Any new kernel log lines contain the error message? */
	if (klog_new_text &&
	    strstr(klog_new_text + lastline_len,
		  "Error attempting to write lower page"))
		rc = TEST_FAILED;

	free(klog_before);
	free(klog_after);
tidy:
	unlink(argv[1]);

	exit(rc);
}