1 | mount --make-shared <mount-object> |
1 | mount --make-shared / |
1 | mount --make-slave <shared-mount-object> |
1 | mount --make-private <mount-object> |
1 | mount --make-unbindable <mount-object> |
1 | mount --make-rshared / |
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 | #define DIRNAMSZ 200 int handle_login(const char *user) { int ret = 0; struct stat statbuf; char dirnam[DIRNAMSZ]; if (strcmp(user, "root") == 0) return PAM_SUCCESS; ret = unshare(CLONE_NEWNS); if (ret) { mysyslog(LOG_ERR, "failed to unshare mounts for %s\n", user); return PAM_SESSION_ERR; } snprintf(dirnam, DIRNAMSZ, "/tmp/priv/%s", user); ret = stat(dirnam, &statbuf); if (ret == 0 && S_ISDIR(statbuf.st_mode)) { ret = mount(dirnam, "/tmp", "none", MS_BIND, NULL); if (ret) { mysyslog(LOG_ERR, "failed to mount tmp for %s\n", user); return PAM_SESSION_ERR; } } else mysyslog(LOG_INFO, "No private /tmp for user %s\n", user); return PAM_SUCCESS; } int pam_sm_open_session(pam_handle_t *pamh, int flags, int argc, const char **argv) { const char *PAM_user = NULL; char *fnam; int ret; ret = pam_get_user(pamh, &PAM_user, NULL); if (ret != PAM_SUCCESS) { mysyslog(LOG_ERR, "PAM-NS: couldn't get user\n"); return PAM_SESSION_ERR; } return handle_login(PAM_user); } |
1 | session required pam_ns.so |
1 2 3 4 | mkdir /tmp/priv chmod 000 /tmp/priv mkdir /tmp/priv/USER chown -R USER /tmp/priv/USER |
1 2 | touch /tmp/ab ls /tmp |
1 | ls /tmp/priv/USER |
1 | mount --bind /dev /mnt |
1 | mount --make-rshared / |
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 | #define DIRNAMSZ 200 #ifndef MS_SLAVE #define MS_SLAVE 1<<19 #endif #ifndef MS_REC #define MS_REC 0x4000 #endif int handle_login(const char *user) { int ret = 0; struct stat statbuf; char dirnam[DIRNAMSZ]; if (strcmp(user, "root") == 0) return PAM_SUCCESS; ret = unshare(CLONE_NEWNS); if (ret) { mysyslog(LOG_ERR, "failed to unshare mounts for %s\n", user); return PAM_SESSION_ERR; } ret = mount("", "/", "dontcare", MS_REC|MS_SLAVE, "")); if (ret) { mysyslog(LOG_ERR, "failed to mark / rslave for %s\n", user); return PAM_SESSION_ERR; } snprintf(dirnam, DIRNAMSZ, "/tmp/priv/%s", user); ret = stat(dirnam, &statbuf); if (ret == 0 && S_ISDIR(statbuf.st_mode)) { ret = mount(dirnam, "/tmp", "none", MS_BIND, NULL); if (ret) { mysyslog(LOG_ERR, "failed to mount tmp for %s\n", user); return PAM_SESSION_ERR; } } else mysyslog(LOG_INFO, "No private /tmp for user %s\n", user); return PAM_SUCCESS; } |
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/) | Powered by Discuz! 7.0.0 |