• Roberto Sassu's avatar
    ima: fix race condition on ima_rdwr_violation_check and process_measurement · f7a859ff
    Roberto Sassu authored
    This patch fixes a race condition between two functions that try to access
    the same inode. Since the i_mutex lock is held and released separately
    in the two functions, there may be the possibility that a violation is
    not correctly detected.
    
    Suppose there are two processes, A (reader) and B (writer), if the
    following sequence happens:
    
    A: ima_rdwr_violation_check()
    B: ima_rdwr_violation_check()
    B: process_measurement()
    B: starts writing the inode
    A: process_measurement()
    
    the ToMToU violation (a reader may be accessing a content different from
    that measured, due to a concurrent modification by a writer) will not be
    detected. To avoid this issue, the violation check and the measurement
    must be done atomically.
    
    This patch fixes the problem by moving the violation check inside
    process_measurement() when the i_mutex lock is held. Differently from
    the old code, the violation check is executed also for the MMAP_CHECK
    hook (other than for FILE_CHECK). This allows to detect ToMToU violations
    that are possible because shared libraries can be opened for writing
    while they are in use (according to the output of 'man mmap', the mmap()
    flag MAP_DENYWRITE is ignored).
    
    Changes in v5 (Roberto Sassu):
    * get iint if action is not zero
    * exit process_measurement() after the violation check if action is zero
    * reverse order process_measurement() exit cleanup (Mimi)
    
    Changes in v4 (Dmitry Kasatkin):
    * iint allocation is done before calling ima_rdrw_violation_check()
      (Suggested-by Mimi)
    * do not check for violations if the policy does not contain 'measure'
      rules (done by Roberto Sassu)
    
    Changes in v3 (Dmitry Kasatkin):
    * no violation checking for MMAP_CHECK function in this patch
    * remove use of filename from violation
    * removes checking if ima is enabled from ima_rdrw_violation_check
    * slight style change
    Suggested-by: default avatarDmitry Kasatkin <d.kasatkin@samsung.com>
    Signed-off-by: default avatarRoberto Sassu <roberto.sassu@polito.it>
    Signed-off-by: default avatarDmitry Kasatkin <d.kasatkin@samsung.com>
    Signed-off-by: default avatarMimi Zohar <zohar@linux.vnet.ibm.com>
    f7a859ff
ima_main.c 9.51 KB