Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752955AbdLDMGa (ORCPT ); Mon, 4 Dec 2017 07:06:30 -0500 Received: from mx0b-001b2d01.pphosted.com ([148.163.158.5]:36818 "EHLO mx0a-001b2d01.pphosted.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1752503AbdLDMGZ (ORCPT ); Mon, 4 Dec 2017 07:06:25 -0500 Subject: Re: [PATCHv5 1/1] ima: re-introduce own integrity cache lock From: Mimi Zohar To: Dmitry Kasatkin , linux-integrity@vger.kernel.org, linux-security-module@vger.kernel.org Cc: linux-kernel@vger.kernel.org, hch@infradead.org, linux-fsdevel@vger.kernel.org, Dmitry Kasatkin Date: Mon, 04 Dec 2017 07:06:13 -0500 In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" X-Mailer: Evolution 3.20.5 (3.20.5-1.fc24) Mime-Version: 1.0 Content-Transfer-Encoding: 8bit X-TM-AS-GCONF: 00 x-cbid: 17120412-0016-0000-0000-000005092141 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 17120412-0017-0000-0000-000028451774 Message-Id: <1512389173.3977.26.camel@linux.vnet.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:,, definitions=2017-12-04_03:,, signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 malwarescore=0 suspectscore=1 phishscore=0 bulkscore=0 spamscore=0 clxscore=1011 lowpriorityscore=0 impostorscore=0 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1709140000 definitions=main-1712040176 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 2961 Lines: 60 Hi Dmitry, On Fri, 2017-12-01 at 20:40 +0200, Dmitry Kasatkin wrote: > The original design was discussed 3+ years ago, but was never completed/upstreamed. > Based on the recent discussions with Linus > https://patchwork.kernel.org/patch/9975919, I've rebased this patch. > > Before IMA appraisal was introduced, IMA was using own integrity cache > lock along with i_mutex. process_measurement and ima_file_free took > the iint->mutex first and then the i_mutex, while setxattr, chmod and > chown took the locks in reverse order. To resolve the potential deadlock, > i_mutex was moved to protect entire IMA functionality and the redundant > iint->mutex was eliminated. > > Solution was based on the assumption that filesystem code does not take > i_mutex further. But when file is opened with O_DIRECT flag, direct-io > implementation takes i_mutex and produces deadlock. Furthermore, certain > other filesystem operations, such as llseek, also take i_mutex. > > More recently some filesystems have replaced their filesystem specific > lock with the global i_rwsem to read a file. As a result, when IMA > attempts to calculate the file hash, reading the file attempts to take > the i_rwsem again. > > To resolve O_DIRECT related deadlock problem, this patch re-introduces > iint->mutex. But to eliminate the original chmod() related deadlock > problem, this patch eliminates the requirement for chmod hooks to take > the iint->mutex by introducing additional atomic iint->attr_flags to > indicate calling of the hooks. The allowed locking order is to take > the iint->mutex first and then the i_rwsem. > > Original flags were cleared in chmod(), setxattr() or removwxattr() hooks > and tested when file was closed or opened again. New atomic flags are set > or cleared in those hooks and tested to clear iint->flags on close or on open. > > Atomic flags are following: > * IMA_CHANGE_ATTR - indicates that chATTR() was called (chmod, chown, chgrp) > and file attributes have changed. On file open, it causes IMA to clear > iint->flags to re-evaluate policy and perform IMA functions again. > * IMA_CHANGE_XATTR - indicates that setxattr or removexattr was called and > extended attributes have changed. On file open, it causes IMA to clear > iint->flags IMA_DONE_MASK to re-appraise. > * IMA_UPDATE_XATTR - indicates that security.ima needs to be updated. > It is cleared if file policy changes and no update is needed. > * IMA_DIGSIG - indicates that file security.ima has signature and file > security.ima must not update to file has on file close. Nice!  I've been testing with this patch and all seems good.  Before queueing this patch to be upstreamed, I'd appreciate if others tested using it as well.  It applies cleanly to the next-queued branch. A subsequent patch will remove the O_DIRECT check in ima_calc_file_hash(). Fixes: Commit 6552321831dc "xfs: remove i_iolock and use i_rwsem in the VFS inode instead" thanks, Mimi