Commit message: None
CVE: CVE-2017-8849
CWE: CWE-20
Vulnerability description: smb4k before 2.0.1 allows local users to gain root privileges by leveraging failure to verify arguments to the mount helper DBUS service.

Project: kde

bool Smb4KGlobal::addMountedShare(Smb4KShare *share)
{
  Q_ASSERT(share);

  bool added = false;

  if (share)
  {
    mutex.lock();

    if (!findShareByPath(share->path()))
    {
      p->mountedSharesList.append(share);
      added = true;

      p->onlyForeignShares = true;
    
      for (Smb4KShare *s : p->mountedSharesList)
      {
        if (!s->isForeign())
        {
          p->onlyForeignShares = false;
          break;
        }
        else
        {
        }
      }
    }
    else
    {
    }

    mutex.unlock();
  }
  else
  {
  }

  return added;
}

Project: kde

bool Smb4KGlobal::addShare(Smb4KShare *share)
{
  Q_ASSERT(share);

  bool added = false;
  
  if (share)
  {
    mutex.lock();

    if (!findShare(share->unc(), share->workgroupName()))
    {
      p->sharesList.append(share);
      added = true;
    }
    else
    {
    }
  }
  else
  {
  }

  mutex.unlock();

  return added;
}

Project: kde

bool Smb4KGlobal::addWorkgroup(Smb4KWorkgroup *workgroup)
{
  Q_ASSERT(workgroup);

  bool added = false;

  mutex.lock();

  if (!findWorkgroup(workgroup->workgroupName()))
  {
    p->workgroupsList.append(workgroup);
    added = true;
  }
  else
  {
  }

  mutex.unlock();

  return added;
}

Project: kde

void Smb4KGlobal::clearSharesList()
{
  mutex.lock();

  while (!p->sharesList.isEmpty())
  {
    delete p->sharesList.takeFirst();
  }

  mutex.unlock();
}

Project: kde

QList<Smb4KShare*> Smb4KGlobal::findInaccessibleShares()
{
  QList<Smb4KShare *> inaccessibleShares;

  mutex.lock();
  
  for (Smb4KShare *s : p->mountedSharesList)
  {
    if (s->isInaccessible())
    {
      inaccessibleShares += s;
    }
    else
    {
    }
  }

  mutex.unlock();

  return inaccessibleShares;
}

Project: kde

Smb4KShare *Smb4KGlobal::findShare(const QString& unc, const QString& workgroup)
{
  Smb4KShare *share = 0;
  
  mutex.lock();

  for (Smb4KShare *s : p->sharesList)
  {
    if (QString::compare(s->unc(), unc, Qt::CaseInsensitive) == 0 &&
        (workgroup.isEmpty() || QString::compare(s->workgroupName(), workgroup, Qt::CaseInsensitive) == 0))
    {
      share = s;
      break;
    }
    else
    {
    }
  }
  
  mutex.unlock();
  
  return share;
}

Project: kde

Smb4KShare* Smb4KGlobal::findShareByPath(const QString &path)
{
  Smb4KShare *share = 0;

  mutex.lock();

  if (!path.isEmpty() && !p->mountedSharesList.isEmpty())
  {
    for (Smb4KShare *s : p->mountedSharesList)
    {
      if (QString::compare(s->path(), path, Qt::CaseInsensitive) == 0 ||
          QString::compare(s->canonicalPath(), path, Qt::CaseInsensitive) == 0)
      {
        share = s;
        break;
      }
      else
      {
      }
    }
  }
  else
  {
  }

  mutex.unlock();

  return share;
}

Project: kde

QList<Smb4KShare *> Smb4KGlobal::findShareByUNC(const QString &unc)
{
  QList<Smb4KShare *> shares;

  mutex.lock();

  if (!unc.isEmpty() && !p->mountedSharesList.isEmpty())
  {
    for (Smb4KShare *s : p->mountedSharesList)
    {
      if (QString::compare(s->unc(), unc, Qt::CaseInsensitive) == 0)
      {
        shares += s;
      }
      else
      {
      }
    }
  }
  else
  {
  }

  mutex.unlock();

  return shares;
}

Project: kde

void Smb4KGlobal::openShare(Smb4KShare *share, OpenWith openWith)
{
  if (!share || share->isInaccessible())
  {
    return;
  }

  switch (openWith)
  {
    case FileManager:
    {
      QUrl url = QUrl::fromLocalFile(share->canonicalPath());

      (void) new KRun(url, 0);

      break;
    }
    case Konsole:
    {
      QString konsole = QStandardPaths::findExecutable("konsole");

      if (konsole.isEmpty())
      {
        Smb4KNotification::commandNotFound("konsole");
      }
      else
      {
        KRun::runCommand(konsole+" --workdir "+KShell::quoteArg(share->canonicalPath()), 0);
      }

      break;
    }
    default:
    {
      break;
    }
  }
}

Project: kde

bool Smb4KGlobal::removeMountedShare(Smb4KShare *share)
{
  Q_ASSERT(share);

  bool removed = false;
  
  if (share)
  {
    mutex.lock();
  
    QMutableListIterator<Smb4KShare *> it(p->mountedSharesList);
    
    while (it.hasNext())
    {
      Smb4KShare *s = it.next();
      
      if (QString::compare(s->path(), share->path(), Qt::CaseInsensitive) == 0 ||
          QString::compare(s->canonicalPath(), share->canonicalPath(), Qt::CaseInsensitive) == 0)
      {
        it.remove();
        removed = true;
        break;
      }
      else
      {
      }
    }
    
    for (Smb4KShare *s : p->mountedSharesList)
    {
      if (!s->isForeign())
      {
        p->onlyForeignShares = false;
        break;
      }
      else
      {
      }
    }
    
    delete share;
    
    mutex.unlock();
  }
  else
  {
  }
  
  return removed;
}

Project: kde

bool Smb4KGlobal::removeShare(Smb4KShare *share)
{
  Q_ASSERT(share);

  bool removed = false;
  
  if (share)
  {
    mutex.lock();

    int index = p->sharesList.indexOf(share);

    if (index != -1)
    {
      delete p->sharesList.takeAt(index);
      removed = true;
    }
    else
    {
      Smb4KShare *s = findShare(share->unc(), share->workgroupName());

      if (s)
      {
        index = p->sharesList.indexOf(s);

        if (index != -1)
        {
          delete p->sharesList.takeAt(index);
          removed = true;
        }
        else
        {
        }
      }
      else
      {
      }

      delete share;
    }

    mutex.unlock();
  }
  else
  {
  }

  return removed;
}

Project: kde

bool Smb4KGlobal::removeWorkgroup(Smb4KWorkgroup *workgroup)
{
  Q_ASSERT(workgroup);

  bool removed = false;

  mutex.lock();

  int index = p->workgroupsList.indexOf(workgroup);

  if (index != -1)
  {
    delete p->workgroupsList.takeAt(index);
    removed = true;
  }
  else
  {
    Smb4KWorkgroup *wg = findWorkgroup(workgroup->workgroupName());

    if (wg)
    {
      index = p->workgroupsList.indexOf(wg);

      if (index != -1)
      {
        delete p->workgroupsList.takeAt(index);
        removed = true;
      }
      else
      {
      }
    }
    else
    {
    }

    delete workgroup;
  }

  mutex.unlock();

  return removed;
}

Project: kde

QList<Smb4KShare *> Smb4KGlobal::sharedResources(Smb4KHost *host)
{
  QList<Smb4KShare *> shares;

  mutex.lock();
  
  for (Smb4KShare *s : p->sharesList)
  {
    if (QString::compare(s->hostName(), host->hostName(), Qt::CaseInsensitive) == 0 &&
        QString::compare(s->workgroupName(), host->workgroupName(), Qt::CaseInsensitive) == 0)
    {
      shares += s;
    }
    else
    {
    }
  }

  mutex.unlock();

  return shares;
}

Project: kde

QList<Smb4KHost *> Smb4KGlobal::workgroupMembers(Smb4KWorkgroup *workgroup)
{
  QList<Smb4KHost *> hosts;

  mutex.lock();
  
  for (Smb4KHost *h : p->hostsList)
  {
    if (QString::compare(h->workgroupName(), workgroup->workgroupName(), Qt::CaseInsensitive) == 0)
    {
      hosts += h;
    }
    else
    {
    }
  }

  mutex.unlock();

  return hosts;
}

Project: kde

void Smb4KMounter::abortAll()
{
  if (!QCoreApplication::closingDown())
  {
    QListIterator<KJob *> it(subjobs());
    
    while (it.hasNext())
    {
      it.next()->kill(KJob::EmitResult);
    }
  }
  else
  {
  }
}