C++程序  |  67行  |  1.63 KB

/*
 * Copyright (c) 2017 FUJITSU LIMITED
 * Author: Guangwen Feng <fenggw-fnst@cn.fujitsu.com>
 *
 * 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, see <http://www.gnu.org/licenses/>.
 */

/*
 * Test for CVE-2017-2618, this regression test can crash
 * the buggy kernel, and the bug was fixed in:
 *
 *  commit 0c461cb727d146c9ef2d3e86214f498b78b7d125
 *  Author: Stephen Smalley <sds@tycho.nsa.gov>
 *  Date:   Tue Jan 31 11:54:04 2017 -0500
 *
 *  selinux: fix off-by-one in setprocattr
 */

#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include "tst_test.h"

#define LOOPS	100
#define PATH_ATTRFS	"/proc/self/attr/fscreate"

static void setup(void)
{
	if (access(PATH_ATTRFS, F_OK))
		tst_brk(TCONF, "%s does not exist", PATH_ATTRFS);
}

static void do_test(void)
{
	int i, fd;

	for (i = 0; i < LOOPS; i++) {
		if (!SAFE_FORK()) {
			fd = SAFE_OPEN(PATH_ATTRFS, O_WRONLY);
			write(fd, "\n", 1);
			SAFE_CLOSE(fd);
			exit(0);
		}

		tst_reap_children();
	}

	tst_res(TPASS, "Bug not reproduced");
}

static struct tst_test test = {
	.forks_child = 1,
	.setup = setup,
	.test_all = do_test,
};