11d89ad56359b7771d3d90a534a47c97259d4a723robbiew/*
21d89ad56359b7771d3d90a534a47c97259d4a723robbiew * Copyright (c) Wipro Technologies Ltd, 2002.  All Rights Reserved.
31d89ad56359b7771d3d90a534a47c97259d4a723robbiew *
41d89ad56359b7771d3d90a534a47c97259d4a723robbiew * This program is free software; you can redistribute it and/or modify it
51d89ad56359b7771d3d90a534a47c97259d4a723robbiew * under the terms of version 2 of the GNU General Public License as
61d89ad56359b7771d3d90a534a47c97259d4a723robbiew * published by the Free Software Foundation.
71d89ad56359b7771d3d90a534a47c97259d4a723robbiew *
81d89ad56359b7771d3d90a534a47c97259d4a723robbiew * This program is distributed in the hope that it would be useful, but
91d89ad56359b7771d3d90a534a47c97259d4a723robbiew * WITHOUT ANY WARRANTY; without even the implied warranty of
101d89ad56359b7771d3d90a534a47c97259d4a723robbiew * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
111d89ad56359b7771d3d90a534a47c97259d4a723robbiew *
121d89ad56359b7771d3d90a534a47c97259d4a723robbiew * You should have received a copy of the GNU General Public License along
13fed9641096e27f79a0f2d9adfe9839dd8d11dc0fWanlong Gao * with this program; if not, write the Free Software Foundation, Inc.,
14fed9641096e27f79a0f2d9adfe9839dd8d11dc0fWanlong Gao * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
151d89ad56359b7771d3d90a534a47c97259d4a723robbiew *
16629a1ac67fb99e7d44318a7c548cfa5815dd4ebbCyril Hrubis * DESCRIPTION
171d89ad56359b7771d3d90a534a47c97259d4a723robbiew *	Verify that mount(2) returns -1 and sets errno to  EPERM if the user
181d89ad56359b7771d3d90a534a47c97259d4a723robbiew *	is not the super-user.
19629a1ac67fb99e7d44318a7c548cfa5815dd4ebbCyril Hrubis */
201d89ad56359b7771d3d90a534a47c97259d4a723robbiew
211d89ad56359b7771d3d90a534a47c97259d4a723robbiew#include <errno.h>
221d89ad56359b7771d3d90a534a47c97259d4a723robbiew#include <sys/mount.h>
231d89ad56359b7771d3d90a534a47c97259d4a723robbiew#include <sys/types.h>
241d89ad56359b7771d3d90a534a47c97259d4a723robbiew#include <sys/stat.h>
25a7fa8561a5a095faf6e202a80b0c7729be8bb82dSteven Jackson#include <fcntl.h>
261d89ad56359b7771d3d90a534a47c97259d4a723robbiew#include <pwd.h>
271d89ad56359b7771d3d90a534a47c97259d4a723robbiew#include "test.h"
281c9f529dce08c061a498a0ff4ba3154509be5ea1Cyril Hrubis#include "safe_macros.h"
291d89ad56359b7771d3d90a534a47c97259d4a723robbiew
301d89ad56359b7771d3d90a534a47c97259d4a723robbiewstatic void setup(void);
311d89ad56359b7771d3d90a534a47c97259d4a723robbiewstatic void cleanup(void);
321d89ad56359b7771d3d90a534a47c97259d4a723robbiew
33fdce7d5e2a219d201a2b0e3bab6b61b01ec1d716Cyril Hrubischar *TCID = "mount04";
341d89ad56359b7771d3d90a534a47c97259d4a723robbiew
351d89ad56359b7771d3d90a534a47c97259d4a723robbiew#define DIR_MODE	S_IRWXU | S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP
361d89ad56359b7771d3d90a534a47c97259d4a723robbiew
37629a1ac67fb99e7d44318a7c548cfa5815dd4ebbCyril Hrubisstatic char *mntpoint = "mntpoint";
381c9f529dce08c061a498a0ff4ba3154509be5ea1Cyril Hrubisstatic const char *fs_type;
391c9f529dce08c061a498a0ff4ba3154509be5ea1Cyril Hrubisstatic const char *device;
401d89ad56359b7771d3d90a534a47c97259d4a723robbiew
411c9f529dce08c061a498a0ff4ba3154509be5ea1Cyril Hrubisint TST_TOTAL = 1;
421d89ad56359b7771d3d90a534a47c97259d4a723robbiew
431c9f529dce08c061a498a0ff4ba3154509be5ea1Cyril Hrubisstatic void verify_mount(void)
441c9f529dce08c061a498a0ff4ba3154509be5ea1Cyril Hrubis{
451c9f529dce08c061a498a0ff4ba3154509be5ea1Cyril Hrubis
461c9f529dce08c061a498a0ff4ba3154509be5ea1Cyril Hrubis	TEST(mount(device, mntpoint, fs_type, 0, NULL));
471c9f529dce08c061a498a0ff4ba3154509be5ea1Cyril Hrubis
481c9f529dce08c061a498a0ff4ba3154509be5ea1Cyril Hrubis	if (TEST_RETURN == -1) {
491c9f529dce08c061a498a0ff4ba3154509be5ea1Cyril Hrubis		if (TEST_ERRNO == EPERM)
501c9f529dce08c061a498a0ff4ba3154509be5ea1Cyril Hrubis			tst_resm(TPASS | TTERRNO, "mount() failed expectedly");
511c9f529dce08c061a498a0ff4ba3154509be5ea1Cyril Hrubis		else
521c9f529dce08c061a498a0ff4ba3154509be5ea1Cyril Hrubis			tst_resm(TFAIL | TTERRNO,
531c9f529dce08c061a498a0ff4ba3154509be5ea1Cyril Hrubis			         "mount() was expected to fail with EPERM");
541c9f529dce08c061a498a0ff4ba3154509be5ea1Cyril Hrubis		return;
551c9f529dce08c061a498a0ff4ba3154509be5ea1Cyril Hrubis	}
561c9f529dce08c061a498a0ff4ba3154509be5ea1Cyril Hrubis
571c9f529dce08c061a498a0ff4ba3154509be5ea1Cyril Hrubis	if (TEST_RETURN == 0) {
581c9f529dce08c061a498a0ff4ba3154509be5ea1Cyril Hrubis		tst_resm(TFAIL, "mount() succeeded unexpectedly");
59c60a23034267b51ba36459a605592f4d3bc4172bCyril Hrubis		if (tst_umount(mntpoint))
601c9f529dce08c061a498a0ff4ba3154509be5ea1Cyril Hrubis			tst_brkm(TBROK, cleanup, "umount() failed");
611c9f529dce08c061a498a0ff4ba3154509be5ea1Cyril Hrubis		return;
621c9f529dce08c061a498a0ff4ba3154509be5ea1Cyril Hrubis	}
631c9f529dce08c061a498a0ff4ba3154509be5ea1Cyril Hrubis
641c9f529dce08c061a498a0ff4ba3154509be5ea1Cyril Hrubis	tst_resm(TFAIL | TTERRNO, "mount() returned %li", TEST_RETURN);
651c9f529dce08c061a498a0ff4ba3154509be5ea1Cyril Hrubis}
661d89ad56359b7771d3d90a534a47c97259d4a723robbiew
6756207cec7732e09c216c751c0b5f88a242bacae6subrata_modakint main(int ac, char **av)
681d89ad56359b7771d3d90a534a47c97259d4a723robbiew{
691c9f529dce08c061a498a0ff4ba3154509be5ea1Cyril Hrubis	int lc;
701d89ad56359b7771d3d90a534a47c97259d4a723robbiew
71d6d11d08678aac1ed2c370ea8e42e5f45aea07beCyril Hrubis	tst_parse_opts(ac, av, NULL, NULL);
721d89ad56359b7771d3d90a534a47c97259d4a723robbiew
731d89ad56359b7771d3d90a534a47c97259d4a723robbiew	setup();
741d89ad56359b7771d3d90a534a47c97259d4a723robbiew
751d89ad56359b7771d3d90a534a47c97259d4a723robbiew	for (lc = 0; TEST_LOOPING(lc); lc++) {
76d59a659cd639ca2780b00049d102acd2a783d585Caspar Zhang		tst_count = 0;
771c9f529dce08c061a498a0ff4ba3154509be5ea1Cyril Hrubis		verify_mount();
782c28215423293e443469a07ae7011135d058b671Garrett Cooper	}
791d89ad56359b7771d3d90a534a47c97259d4a723robbiew
801d89ad56359b7771d3d90a534a47c97259d4a723robbiew	cleanup();
8153740500924f6439623a8ac256b5be2d6c59ed1fGarrett Cooper	tst_exit();
822c28215423293e443469a07ae7011135d058b671Garrett Cooper}
831d89ad56359b7771d3d90a534a47c97259d4a723robbiew
84629a1ac67fb99e7d44318a7c548cfa5815dd4ebbCyril Hrubisstatic void setup(void)
851d89ad56359b7771d3d90a534a47c97259d4a723robbiew{
8656207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	char nobody_uid[] = "nobody";
871d89ad56359b7771d3d90a534a47c97259d4a723robbiew	struct passwd *ltpuser;
881d89ad56359b7771d3d90a534a47c97259d4a723robbiew
891d89ad56359b7771d3d90a534a47c97259d4a723robbiew	tst_sig(FORK, DEF_HANDLER, cleanup);
901d89ad56359b7771d3d90a534a47c97259d4a723robbiew
91d1e794d62b1bf619df8390535e4c2a58899b1145Cyril Hrubis	tst_require_root();
921d89ad56359b7771d3d90a534a47c97259d4a723robbiew
93629a1ac67fb99e7d44318a7c548cfa5815dd4ebbCyril Hrubis	tst_tmpdir();
941d89ad56359b7771d3d90a534a47c97259d4a723robbiew
951c9f529dce08c061a498a0ff4ba3154509be5ea1Cyril Hrubis	fs_type = tst_dev_fs_type();
961c9f529dce08c061a498a0ff4ba3154509be5ea1Cyril Hrubis	device = tst_acquire_device(cleanup);
971c9f529dce08c061a498a0ff4ba3154509be5ea1Cyril Hrubis
981c9f529dce08c061a498a0ff4ba3154509be5ea1Cyril Hrubis	if (!device)
991c9f529dce08c061a498a0ff4ba3154509be5ea1Cyril Hrubis		tst_brkm(TCONF, cleanup, "Failed to obtain block device");
1001c9f529dce08c061a498a0ff4ba3154509be5ea1Cyril Hrubis
101d5e1788b647aca13ee89f41c08113d7b5fa1c499Zorro Lang	tst_mkfs(cleanup, device, fs_type, NULL, NULL);
1021c9f529dce08c061a498a0ff4ba3154509be5ea1Cyril Hrubis
1031c9f529dce08c061a498a0ff4ba3154509be5ea1Cyril Hrubis	ltpuser = SAFE_GETPWNAM(cleanup, nobody_uid);
1041c9f529dce08c061a498a0ff4ba3154509be5ea1Cyril Hrubis	SAFE_SETEUID(cleanup, ltpuser->pw_uid);
1051c9f529dce08c061a498a0ff4ba3154509be5ea1Cyril Hrubis	SAFE_MKDIR(cleanup, mntpoint, DIR_MODE);
1061d89ad56359b7771d3d90a534a47c97259d4a723robbiew
1071d89ad56359b7771d3d90a534a47c97259d4a723robbiew	TEST_PAUSE;
1082c28215423293e443469a07ae7011135d058b671Garrett Cooper}
1091d89ad56359b7771d3d90a534a47c97259d4a723robbiew
110629a1ac67fb99e7d44318a7c548cfa5815dd4ebbCyril Hrubisstatic void cleanup(void)
1111d89ad56359b7771d3d90a534a47c97259d4a723robbiew{
1121c9f529dce08c061a498a0ff4ba3154509be5ea1Cyril Hrubis	if (seteuid(0))
1131c9f529dce08c061a498a0ff4ba3154509be5ea1Cyril Hrubis		tst_resm(TWARN | TERRNO, "seteuid(0) failed");
1141d89ad56359b7771d3d90a534a47c97259d4a723robbiew
1151c9f529dce08c061a498a0ff4ba3154509be5ea1Cyril Hrubis	if (device)
116bbdb9f78378c7e038f463efa39d2470e1c51ad54Cyril Hrubis		tst_release_device(device);
1171d89ad56359b7771d3d90a534a47c97259d4a723robbiew
1181c9f529dce08c061a498a0ff4ba3154509be5ea1Cyril Hrubis	tst_rmdir();
119ec6edca7aa42b6affd989ef91b5897f96795e40fChris Dearman}
120