| 3728 | */ |
| 3729 | |
| 3730 | static int |
| 3731 | coredump(struct thread *td) |
| 3732 | { |
| 3733 | struct proc *p = td->td_proc; |
| 3734 | struct ucred *cred = td->td_ucred; |
| 3735 | struct vnode *vp; |
| 3736 | struct flock lf; |
| 3737 | struct vattr vattr; |
| 3738 | size_t fullpathsize; |
| 3739 | int error, error1, locked; |
| 3740 | char *name; /* name of corefile */ |
| 3741 | void *rl_cookie; |
| 3742 | off_t limit; |
| 3743 | char *fullpath, *freepath = NULL; |
| 3744 | struct sbuf *sb; |
| 3745 | |
| 3746 | PROC_LOCK_ASSERT(p, MA_OWNED); |
| 3747 | MPASS((p->p_flag & P_HADTHREADS) == 0 || p->p_singlethread == td); |
| 3748 | |
| 3749 | if (!do_coredump || (!sugid_coredump && (p->p_flag & P_SUGID) != 0) || |
| 3750 | (p->p_flag2 & P2_NOTRACE) != 0) { |
| 3751 | PROC_UNLOCK(p); |
| 3752 | return (EFAULT); |
| 3753 | } |
| 3754 | |
| 3755 | /* |
| 3756 | * Note that the bulk of limit checking is done after |
| 3757 | * the corefile is created. The exception is if the limit |
| 3758 | * for corefiles is 0, in which case we don't bother |
| 3759 | * creating the corefile at all. This layout means that |
| 3760 | * a corefile is truncated instead of not being created, |
| 3761 | * if it is larger than the limit. |
| 3762 | */ |
| 3763 | limit = (off_t)lim_cur(td, RLIMIT_CORE); |
| 3764 | if (limit == 0 || racct_get_available(p, RACCT_CORE) == 0) { |
| 3765 | PROC_UNLOCK(p); |
| 3766 | return (EFBIG); |
| 3767 | } |
| 3768 | PROC_UNLOCK(p); |
| 3769 | |
| 3770 | error = corefile_open(p->p_comm, cred->cr_uid, p->p_pid, td, |
| 3771 | compress_user_cores, p->p_sig, &vp, &name); |
| 3772 | if (error != 0) |
| 3773 | return (error); |
| 3774 | |
| 3775 | /* |
| 3776 | * Don't dump to non-regular files or files with links. |
| 3777 | * Do not dump into system files. Effective user must own the corefile. |
| 3778 | */ |
| 3779 | if (vp->v_type != VREG || VOP_GETATTR(vp, &vattr, cred) != 0 || |
| 3780 | vattr.va_nlink != 1 || (vp->v_vflag & VV_SYSTEM) != 0 || |
| 3781 | vattr.va_uid != cred->cr_uid) { |
| 3782 | VOP_UNLOCK(vp); |
| 3783 | error = EFAULT; |
| 3784 | goto out; |
| 3785 | } |
| 3786 | |
| 3787 | VOP_UNLOCK(vp); |
no test coverage detected