1  /* { dg-do compile } */
       2  // TODO: remove need for -fanalyzer-checker=taint here:
       3  /* { dg-options "-fanalyzer -fanalyzer-checker=taint" } */
       4  /* { dg-require-effective-target analyzer } */
       5  
       6  /* See notes in this header.  */
       7  #include "taint-CVE-2011-0521.h"
       8  
       9  /* Adapted from drivers/media/dvb/ttpci/av7110_ca.c  */
      10  
      11  int dvb_ca_ioctl(struct file *file, unsigned int cmd, void *parg)
      12  {
      13  	struct dvb_device *dvbdev = file->private_data;
      14  	struct av7110 *av7110 = dvbdev->priv;
      15  	unsigned long arg = (unsigned long) parg;
      16  
      17  	/* case CA_GET_SLOT_INFO:  */
      18  	{
      19  		ca_slot_info_t *info=(ca_slot_info_t *)parg;
      20  
      21  		if (info->num > 1)
      22  			return -EINVAL;
      23  		av7110->ci_slot[info->num].num = info->num; /* { dg-warning "attacker-controlled value" "" { xfail *-*-* } } */
      24  		av7110->ci_slot[info->num].type = FW_CI_LL_SUPPORT(av7110->arm_app) ?
      25  							CA_CI_LINK : CA_CI;
      26  		memcpy(info, &av7110->ci_slot[info->num], sizeof(ca_slot_info_t));
      27  	}
      28  	return 0;
      29  }
      30  
      31  /* Adapted from drivers/media/dvb/dvb-core/dvbdev.c
      32     Somewhat simplified: rather than pass in a callback that can
      33     be dvb_ca_ioctl, call dvb_ca_ioctl directly.  */
      34  
      35  static DEFINE_MUTEX(dvbdev_mutex);
      36  
      37  int dvb_usercopy(struct file *file,
      38  		 unsigned int cmd, unsigned long arg)
      39  {
      40  	char    sbuf[128];
      41  	void    *mbuf = NULL;
      42  	void    *parg = NULL;
      43  	int     err  = -1;
      44  
      45  	/*  Copy arguments into temp kernel buffer  */
      46  	switch (_IOC_DIR(cmd)) {
      47  	case _IOC_NONE:
      48  		/*
      49  		 * For this command, the pointer is actually an integer
      50  		 * argument.
      51  		 */
      52  		parg = (void *) arg;
      53  		break;
      54  	case _IOC_READ: /* some v4l ioctls are marked wrong ... */
      55  	case _IOC_WRITE:
      56  	case (_IOC_WRITE | _IOC_READ):
      57  		if (_IOC_SIZE(cmd) <= sizeof(sbuf)) {
      58  			parg = sbuf;
      59  		} else {
      60  			/* too big to allocate from stack */
      61  			mbuf = kmalloc(_IOC_SIZE(cmd),GFP_KERNEL);
      62  			if (NULL == mbuf)
      63  				return -ENOMEM;
      64  			parg = mbuf;
      65  		}
      66  
      67  		err = -EFAULT;
      68  		if (copy_from_user(parg, (void __user *)arg, _IOC_SIZE(cmd)))
      69  			goto out;
      70  		break;
      71  	}
      72  
      73  	/* call driver */
      74  	mutex_lock(&dvbdev_mutex);
      75  	if ((err = dvb_ca_ioctl(file, cmd, parg)) == -ENOIOCTLCMD)
      76  		err = -EINVAL;
      77  	mutex_unlock(&dvbdev_mutex);
      78  
      79  	if (err < 0)
      80  		goto out;
      81  
      82  	/*  Copy results into user buffer  */
      83  	switch (_IOC_DIR(cmd))
      84  	{
      85  	case _IOC_READ:
      86  	case (_IOC_WRITE | _IOC_READ):
      87  		if (copy_to_user((void __user *)arg, parg, _IOC_SIZE(cmd)))
      88  			err = -EFAULT;
      89  		break;
      90  	}
      91  
      92  out:
      93  	kfree(mbuf);
      94  	return err;
      95  }